algorithm - List of random numbers -


i need generate list of numbers (about 120.) numbers range 1 x (max 10), both included. algorithm should use every number equal amount of times, or @ least try, if numbers used once less, that's ok.

this first time have make kind of algorithm, i've created simple once, i'm stumped on how this. tried googling first, though don't know call kind of algorithms, couldn't find anything.

thanks lot!

it sounds want first fill list numbers want , shuffle list. 1 way add each of numbers list , repeat process until list has many items want. after that, randomly shuffle list.

in pseudo-code, generating initial list might this:

list = [] while length(list) < n     in 1, 2, ..., x         if length(list) >= n             break         end if         list.append(i)     end end while 

i leave shuffling part exercise reader.

edit: pointed out in comments above put more smaller numbers larger numbers. if isn't what's desired, iterate on possible numbers in random order. example:

list = [] numbers = shuffle( [1, 2, ..., x] ) while length(list) < n     in 1, 2, ..., x         if length(list) >= n             break         end if         list.append( numbers[i] )     end end while 

i think should remove bias.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -