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

Username:   Password: 
 RegisterRegister   
 Randomizing Integers and eliminating last picked
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Joe Keller




PostPosted: Sat Jan 21, 2012 1:19 pm   Post subject: Randomizing Integers and eliminating last picked

What is it you are trying to achieve?
So I was wondering how you randomized numbers, for example 1 to 6, and if one of them were chosen last time by the generater, don't let it be chosen again.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


loop
        randint (productrand, 1, 6)
        counter := counter + 1
%Lots of code goes here but all i want is for the number that was picked last by randomizer to not be picked again.
exit when counter = 3
end loop


Sponsor
Sponsor
Sponsor
sponsor
Beastinonyou




PostPosted: Sat Jan 21, 2012 1:46 pm   Post subject: Re: Randomizing Integers and eliminating last picked

Well, considering you already understand how to randomize integers, you just need a way to prevent that integer from being picked again.

A simple way to do this is using an array of boolean:

Turing:
var pickedAlready : array 1 .. 6 of boolean
var randomInteger, counter : int := 0

% Set the array false, none of the numbers have been picked
for i : 1 .. 6
     pickedAlready (i) := false
end for

loop
     randint (randomInteger, 1, 6)
     % If that number hasn't been picked before
     if pickedAlready (randomInteger) = false then
          % Continue to do whatever you need.
          counter += 1   % Counter to keep track of how many numbers have been used
          pickedAlready (randomInteger) := true  % Finished with this number, we've used it
     end if
     exit when counter = 6  % Exits when all 6 numbers have been used
end loop
Joe Keller




PostPosted: Sat Jan 21, 2012 1:49 pm   Post subject: RE:Randomizing Integers and eliminating last picked

Thank you, I never used arrays before Ill put them in to my program in a bit and tell you how it went
Tony




PostPosted: Sat Jan 21, 2012 1:54 pm   Post subject: RE:Randomizing Integers and eliminating last picked

it's important to recognize that with this method, picking the last element would just be a loop with 1/6 chance of exiting. This gets prohibitively slow when you are picking elements from a larger set.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: