-7

Anybody know how can I generate random numbers in a specific time period and specific number.

For i.e I want to generate any random number 7 times only in a minute.

I don't know how can I do this using in jquery using setInterval or setTimeout.

Any help would be appreciate.

Thanks All

P_U
  • 97
  • 4
  • 1
    Sounds like a plan! What have you tried? – jdmdevdotnet Feb 14 '18 at 16:31
  • 1
    divide 1 minute by 7 to get the number of seconds and then run code at that interval, and stop when you get to a minute. – ADyson Feb 14 '18 at 16:31
  • I have tried generating random number from post https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript/7228322#7228322 – P_U Feb 14 '18 at 16:32
  • are you programming this for the antikythera machine or what? on a normal computer it takes a fraction of a second to generate 7 random numbers. – I wrestled a bear once. Feb 14 '18 at 16:32
  • Also tried to using setInterval but stuck in how can I generate only 7 times in a minute. – P_U Feb 14 '18 at 16:33
  • well I made you a suggestion, did you think of working that out? – ADyson Feb 14 '18 at 16:33
  • @Occam'sRazor I don't want to generate it at the same time.as I said in a minute. – P_U Feb 14 '18 at 16:34
  • 1
    Pretty sure everyone commenting can surely help you. The issue with your question is that you did not provide what you've tried. Did you get errors? Did it have unexpected results? This isn't a code writing service, and while I'd love the reputation points, I'm not going to do your homework for you. I suggest figuring it out on your own first, and if you run into issues, ask on here and you'll get the help you need. – jdmdevdotnet Feb 14 '18 at 16:39
  • @ADyson pretty sure this guy just wants someone to do it for him. – jdmdevdotnet Feb 14 '18 at 16:40
  • 1
    @jdmdevdotnet I know that's why I haven't answered. The profile says "Team Lead", so not setting much of an example to the team, really :-(. I think they've got enough clues now – ADyson Feb 14 '18 at 16:44

1 Answers1

2

See the comments for an explanation.

/**
 * Generate random numbers at an interval
 * @param perMinute - The number of numbers to generate per minute
 * @param totalNumbers - The total number of numbers to  generate
 * @param minNumber - The minimum number to be generated
 * @param maxNumber - The max number to be generated
 */
function randomNumberAtInterval(perMinute, totalNumbers, minNumber, maxNumber, cb){
    var int = 60000 / perMinute;
    var count = 0;
    var interval = setInterval(()=>{
        cb(Math.random() * (maxNumber - minNumber) + minNumber);
        count++;
        if(count >= totalNumbers) clearInterval(interval);
    }, int);
}

// Will generate 7 numbers per minute 
// until it has generated 14 numbers (so for 2 minutes)
// between 0 and 10
// and it will log the number to the console.
randomNumberAtInterval(7, 14, 0, 10, function(randomNumber){
    console.log(randomNumber);
})
Sébastien
  • 11,860
  • 11
  • 58
  • 78
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116