Welcome to WebmasterWorld Guest from 54.227.68.206

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)

Junior Member

5+ Year Member

joined:Sept 6, 2008
posts: 50
votes: 0


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)

Preferred Member

5+ Year Member

joined:May 29, 2007
posts: 578
votes: 0


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

Junior Member

5+ Year Member

joined:Sept 6, 2008
posts: 50
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


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

Junior Member

5+ Year Member

joined:Sept 6, 2008
posts:50
votes: 0


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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


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)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 0


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.