homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Non-repeating Random Numbers
Random numbers without repeating
j2trumpet




msg:4029108
 12:51 am on Nov 21, 2009 (gmt 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.

 

lavazza




msg:4029140
 3:24 am on Nov 21, 2009 (gmt 0)

If you control the output, then - by definition - your generator will be non-random

j2trumpet




msg:4029159
 4:34 am on Nov 21, 2009 (gmt 0)

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

Can anyone else help me?

daveVk




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

Store the 'used' answers. If you get a used answer try again.

j2trumpet




msg:4029176
 5:59 am on Nov 21, 2009 (gmt 0)

How do I do that?

daveVk




msg:4029191
 6:48 am on Nov 21, 2009 (gmt 0)

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

replace getRand() with your get random code

rocknbil




msg:4029359
 5:09 pm on Nov 21, 2009 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved