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

Username:   Password: 
 RegisterRegister   
 Generate random numbers, no duplicates.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
renegod




PostPosted: Tue Jan 08, 2008 10:13 pm   Post subject: Generate random numbers, no duplicates.

I am taking MDM4U and I am suppose to analyze "probability game".
The game i choose is a variant of bingo.

I need to generate 90 numbers (between 1 .. 90), no duplicates.
Then generate 15 numbers between (1 .. 90, no duplicates. <- got to do that 5 times (players)

After that I will run my procedure which will see which player won.

I got everything down pretty well, except I cant seem to figure out how to generate random numbers with no dupes,

can anyone help, I dont necessarily need the whole program done for me, a flow chart, or some detail instructions and I could do the rest.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Tue Jan 08, 2008 10:34 pm   Post subject: RE:Generate random numbers, no duplicates.

Have an array with 90 elements, each holding a value from 1 - 90. Then take a random element, record it's value, and then remove that element from the list. Rinse, lather and repeat until the desired consistency.
renegod




PostPosted: Tue Jan 08, 2008 10:56 pm   Post subject: Re: Generate random numbers, no duplicates.

Well i was late on reading your reply, but I was able to get it on my own, thank you for the help, but i do not understand what you mean by

"Have an array with 90 elements, each holding a value from 1 - 90. Then take a random element, record it's value, and then remove that element from the list. Rinse, lather and repeat until the desired consistency."
what list?


also heres my code:
Quote:

%program name: random number generator, no dupes
%programer: shpetim salihu

var rint : int := 0 % the randomly generated number
var r90 : array 1 .. 90 of int % the array where all the nubers go (the bingo list)
var yn : int := 0 % was the number a duplicate? yes/no
var sentinel : int := 0 %exit variable for the loop

for a : 1 .. 90 %sets the r90 variable to 0
r90 (a) := 0
end for

for g : 1 .. 90 % main for loop, runs 90 times, g = generate

sentinel := 0 %reset the sentinel value

loop %this loops until a random number that is not a duplicate is generated
exit when sentinel = 1%exit statement
yn := 0 %resets the "was it a dupe, yes/no" value
randint (rint, 1, 90)%generates a random number

for g2 : 1 .. g % loop to check if the number is duplicate
exit when yn = 1 %if it is dupe, it will exit

if rint = r90 (g2) then % is it a dupe?
yn := 1 %
else
yn := 0 %

end if
end for

if yn = 0 then %if its not a dupe, it wil add it to the array
r90 (g) := rint
sentinel := 1 %set the sentinel so that it exits
else
end if
end loop

put " ", r90 (g) .. %displays numbers for the user
end for
zylum




PostPosted: Tue Jan 08, 2008 11:02 pm   Post subject: RE:Generate random numbers, no duplicates.

You can have an array of consisting of the numbers 1..90 and shuffle the array. Then you iterate through the array to generate the next random number.
Clayton




PostPosted: Tue Jan 08, 2008 11:05 pm   Post subject: RE:Generate random numbers, no duplicates.

The list is your array. Although, thinking about it now, zylum's is an excellent way of doing it as well.
Tony




PostPosted: Tue Jan 08, 2008 11:54 pm   Post subject: RE:Generate random numbers, no duplicates.

Clayton - you are effectively shuffling the array just the same. Though you are discarding the element at read, rather than keeping it in the memory and advancing the index.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Nick




PostPosted: Wed Jan 09, 2008 5:34 am   Post subject: RE:Generate random numbers, no duplicates.

I think zylum's way is more efficient though :/ and I never thought of doing it that way Razz

that will help with the card game I'm making!
zylum




PostPosted: Wed Jan 09, 2008 1:14 pm   Post subject: RE:Generate random numbers, no duplicates.

My method is just easier for Turing as you wont have to create a list to use it Wink
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jan 09, 2008 1:29 pm   Post subject: RE:Generate random numbers, no duplicates.

Clayton's implementation would be something like this:
Turing:

var nums : array 1 .. 90 of int
var k : int

for i: 1 .. 90
   nums[i] := i
end for

for decreasing j: 90 .. 1
   k := Rand.Int(1, j)  % get random element
   put nums[k]  % result
   nums[k] := nums[j]  % remove element
end for


@zylum - it's not trivial to shuffle in Turing and will require an extra loop.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Nick




PostPosted: Wed Jan 09, 2008 3:24 pm   Post subject: RE:Generate random numbers, no duplicates.

Tony wouldn't that implemtation have duplicate values?
Tony




PostPosted: Wed Jan 09, 2008 4:00 pm   Post subject: RE:Generate random numbers, no duplicates.

how so? Once the random element k is printed to screen, it's taken out of the list and replaced with another element, not previously picked (well... unless it's replaced with itself).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
renegod




PostPosted: Wed Jan 09, 2008 5:35 pm   Post subject: Re: RE:Generate random numbers, no duplicates.

Tony @ Wed Jan 09, 2008 1:29 pm wrote:
Clayton's implementation would be something like this:
Turing:

var nums : array 1 .. 90 of int
var k : int

for i: 1 .. 90
   nums[i] := i
end for

for decreasing j: 90 .. 1
   k := Rand.Int(1, j)  % get random element
   put nums[k]  % result
   nums[k] := nums[j]  % remove element
end for


@zylum - it's not trivial to shuffle in Turing and will require an extra loop.



wow thats impressive. much better than mine.
thanks for all the help guys.
Nick




PostPosted: Wed Jan 09, 2008 6:13 pm   Post subject: RE:Generate random numbers, no duplicates.

oh I see lol Razz that however won't work for the card game I'm making :/ oh well I already wrote up my shuffling script
sh0td0wn




PostPosted: Mon Jan 14, 2008 9:23 pm   Post subject: Re: Generate random numbers, no duplicates.

hmmm... I'm trying to use the solution provided in the second to be implemented in my Quiz game but no avail... It doesn't stop and there is still duplicates.

The one I did before I saw this was a loop with randint with an if statement that if it generates this number... a question will be asked and a counter will increase. If the counter will reach 10 Qs, the loop will stop.

My problem is the duplicates.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: