Computer Science Canada

[source code] Random Numbers List

Author:  Tony [ Thu Nov 06, 2003 8:05 pm ]
Post subject:  [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 Laughing
code:

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 in this post

Note that discussion started at the time before I modified the algorythm.

Author:  soulgnat [ Wed Jan 07, 2004 8:35 pm ]
Post subject:  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

Author:  Tony [ Wed Jan 07, 2004 10:30 pm ]
Post subject: 

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 Laughing

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

Author:  shorthair [ Tue Jan 13, 2004 7:15 pm ]
Post subject: 

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 ,

Author:  kanetix [ Tue Jan 13, 2004 11:25 pm ]
Post subject:  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) <--- wut does the randN in the brackets do?

Author:  Tony [ Tue Jan 13, 2004 11:40 pm ]
Post subject: 

(randN) is the index of the array. There're some tutorials available on arrays in [Turing Tutorials].

to arrange words alphabetically, you want a to sort them (easiest is the bubble sort) You can use Search to find the source code for it

Author:  Dragsz [ Tue Mar 17, 2015 8:02 pm ]
Post subject:  RE:[source code] Random Numbers List

Is there any way to move "put numbers (randN)" out of the for loop?

Author:  Insectoid [ Thu Mar 19, 2015 3:20 pm ]
Post subject:  RE:[source code] Random Numbers List

Sure, just cut & paste the code to a line outside the loop. I'm not sure that would be so useful though.


: