Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [source code] Random Numbers List
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
soulgnat




PostPosted: 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
Tony




PostPosted: Wed Jan 07, 2004 10:30 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
shorthair




PostPosted: Tue Jan 13, 2004 7:15 pm   Post subject: (No 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 ,
kanetix




PostPosted: 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?
Tony




PostPosted: Tue Jan 13, 2004 11:40 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dragsz




PostPosted: 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?
Insectoid




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: