
-----------------------------------
Tony
Thu Nov 06, 2003 8:05 pm

[source code] Random Numbers List
-----------------------------------
I wrote up this little piece of code for a question on this exact topic. Generating a list of random, non-repeating numbers. Basically you create an array of numbers, randomly pick one and take it out, then pick next one from the remainder.

most efficient way on the block :lol:

var size:int :=10
var randN:int %just to hold the number
var numbers:array 1..size of int
for i:1..size
numbers(i):=i
end for

for decreasing i:size ..1
randN := Rand.Int(1,i)
put numbers(randN)

    numbers(randN):=numbers(i)

end for


discussion of this method VS. just randomly picking numbers is found [url=http://www.compsci.ca/v2/viewtopic.php?t=2838] in this post

Note that discussion started at the time before I modified the algorythm.

-----------------------------------
soulgnat
Wed Jan 07, 2004 8:35 pm

can you explain your code?
-----------------------------------
can you explain a code? im working on a similar thing but mine is for a school assignment to produce winning numbers on a chance game..thanx so much

-----------------------------------
Tony
Wed Jan 07, 2004 10:30 pm


-----------------------------------
well the explonation was done somewhere in [turing help], I just liked the piece of code I wrote so I made another post here... anyways, explanaton is as follows:

it is extreamly inefficient to just randomly keep on picking the number and hope it's a unique (have not been picked before). If you're after 10 unique numbers, you have just 1/10 chance to pick that last number out. Now if you need randomly sort a list of 100 numbers... well... it will take you a long while with 1/100 chance :lol:

So what you do is you select the position of the number in the array of numbers left, rether then the number itself. After each pick, your array list is reduced by one. This continues on untill neeed amount of numbers were picked.

This is much more efficient since the array contains only the numbers that were not yet picked, so you always have 1/1 chance to get the number you want :wink:

-----------------------------------
shorthair
Tue Jan 13, 2004 7:15 pm


-----------------------------------
Tony if you had joined yeterday and that program was writin with your eyes closed on a comadorer 64 , i would give you all my bits but dude ,

-----------------------------------
kanetix
Tue Jan 13, 2004 11:25 pm

newb question
-----------------------------------
I know its not really on topic but for a school project i have to randomize 10 words and the user has to arrange it in alphabetical order, is that possible?

also wut does     put numbers (randN) 