Welcome to WebmasterWorld Guest from 54.198.87.238

Forum Moderators: open

Message Too Old, No Replies

Non-repeating Random Numbers

Random numbers without repeating

     
12:51 am on Nov 21, 2009 (gmt 0)

5+ Year Member



I've got my random number generator created and functioning, but I need it to not repeat any numbers as it runs. How do I do that? Thanks for the help.
3:24 am on Nov 21, 2009 (gmt 0)

5+ Year Member



If you control the output, then - by definition - your generator will be non-random
4:34 am on Nov 21, 2009 (gmt 0)

5+ Year Member



Thanks for the advice, but that doesn't answer my question.

Can anyone else help me?

5:45 am on Nov 21, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Store the 'used' answers. If you get a used answer try again.
5:59 am on Nov 21, 2009 (gmt 0)

5+ Year Member



How do I do that?
6:48 am on Nov 21, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



var used = [];
...
function getUniq(){
var n;
while ( used[ n = getRand() ] ){}
used[ n ] = true;
return n;
}

replace getRand() with your get random code

5:09 pm on Nov 21, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Another way would be to splice/remove the element from the random array until it's length is zero. When it reaches zero length, repopulate it.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month