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

Username:   Password: 
 RegisterRegister   
 Modifying the Random Number Generator Code for my purposes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shan




PostPosted: Sun Jan 23, 2011 11:02 am   Post subject: Modifying the Random Number Generator Code for my purposes

What is it you are trying to achieve?
I am trying to create a program that will display four letters selected from: AEFHLTZG. The letters must be randomly selected and not repeat. The letters will be displayed in large text, centered on the screen with some space between them.

What is the problem you are having?
I cannot figure out how to ensure that none of the letters repeat themselves

Describe what you have tried to solve this problem
I've read all of the material pertaining to random number generators as posted on this Website, read the Help board and worked at it for a few days. Unfortunately, although I've found random number no repetition generators, I cannot decipher what each line of code means for the sake of fixing it for my program.

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

I have not copy/pasted my entire coding, since it is unfortunately nothing more than a big mess. However, I've copied some of the major coding.
First I made an array to contain each of the 8 letters.
Turing:


var oneSH_share : array 1 .. 8 of string
    oneSH_share(1) := "A"
    oneSH_share(2) := "E"
    oneSH_share(3) := "F"
    oneSH_share(4) := "H"
    oneSH_share(5) := "L"
    oneSH_share(6) := "T"
    oneSH_share(7) := "Z"
    oneSH_share(8) := "G"


My goal is to next randomly generate four numbers (between 1 and 8) so that I can display the corresponding four random letters

Turing:


var x_coordinate : int := 130  %x-coordinate of displayed letters
var letter_font: int := Font.New ("Arial:70:bold")

var number : array 1..4 of int
for i:1..8
number(i):=Rand.Int(1,8)

Font.Draw (oneSH_share(number), x_coordinate, 200, letter_font, 19) %draws the letter - note I DID declare letter_font earlier
       
x_coordinate := x_coordinate + 100  %moves the NEXT letter to the right

end for




I also found this code, to avoid repetition, yet I don't know how to modify it for my use:

Turing:

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



Please specify what version of Turing you are using
4.1.1

Any help is INCREDIBLY appreciated.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sun Jan 23, 2011 11:11 am   Post subject: RE:Modifying the Random Number Generator Code for my purposes

Think of it like shuffling a deck instead of randomly selecting a letter. Shuffle the letters in an array, then output them in that order.
Shan




PostPosted: Sun Jan 23, 2011 12:57 pm   Post subject: RE:Modifying the Random Number Generator Code for my purposes

Unfortunately I'm unsure of what you mean by "shuffling" the array?

ps. I apologize for my very limited knowledge of Turing! I'm doing this not for a programming class, but as part of a psychological experiment (I'm very out of my field here)
Insectoid




PostPosted: Sun Jan 23, 2011 1:18 pm   Post subject: RE:Modifying the Random Number Generator Code for my purposes

One way to shuffle an array is to iterate over every index, pick a random index, and swap those 2 indices. This shuffles it like a deck of cards. Since it's all mixed up now, you can just output the array.
Tony




PostPosted: Sun Jan 23, 2011 1:28 pm   Post subject: Re: RE:Modifying the Random Number Generator Code for my purposes

Shan @ Sun Jan 23, 2011 12:57 pm wrote:
I'm doing this not for a programming class, but as part of a psychological experiment (I'm very out of my field here)

How'd you end up choosing Turing for this?

Insectoid @ Sun Jan 23, 2011 1:18 pm wrote:
swap those 2 indices

There's actually a certain bias in this shuffling method. It might look random, but it's not completely.

That quoted code looks like something I might have wrote. To give a hint towards understanding of it -- You have 8 letters to choose from. You somehow pick a letter out of that set, at random. How many letters are you choosing from for the next random pick?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Thu Feb 17, 2011 9:23 pm   Post subject: RE:Modifying the Random Number Generator Code for my purposes

I don't know if it helps, but I would solve it like my Hangman game: I had a string and every time they guessed a letter, i removed it from the string.

In your case what you could do is that when the user inputs a letter, you replace that letter with a random character that probably won't get used, like ` or a -.
Insectoid




PostPosted: Thu Feb 17, 2011 10:54 pm   Post subject: RE:Modifying the Random Number Generator Code for my purposes

This is a tad late to be any help. Odds are OP is no longer even in a CS class.
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  [ 7 Posts ]
Jump to:   


Style:  
Search: