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

Username:   Password: 
 RegisterRegister   
 How do you randomize letters?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dc116




PostPosted: Wed Dec 10, 2008 7:40 pm   Post subject: How do you randomize letters?

I would like to know how to randomize letters of the alphabet. For example, have the program display a random letter between a and z every time.
Sponsor
Sponsor
Sponsor
sponsor
The_Bean




PostPosted: Wed Dec 10, 2008 7:46 pm   Post subject: Re: How do you randomize letters?

Well the letters A-Z are represented by the numbers 65-90 in the ASCII code.
Turing:

chr(num:int)

Will get the character for the certain ASCII code.
Turing:

Rand.Int (num1,num2:int)

Will produce a random number between, and including num1 and num2.
Parker




PostPosted: Thu Dec 11, 2008 8:33 am   Post subject: Re: How do you randomize letters?

This way involves alot more code, but in my opinion is easier to understand.

Turing:


loop
Rand.Int(num, 1,26)

if num = 1 then
put "a"
elsif num = 2 then
put "b"
% continue doing this etc..
end if


This involves alot of if statements but should work Wink
The_Bean




PostPosted: Thu Dec 11, 2008 1:08 pm   Post subject: Re: How do you randomize letters?

Well 1 line verse 54?

Then if you want lower case instead of uppercase you need to change 26 things, and case statements would be better than if's in your code.
Tony




PostPosted: Thu Dec 11, 2008 1:38 pm   Post subject: Re: How do you randomize letters?

Parker @ Thu Dec 11, 2008 8:33 am wrote:
in my opinion is easier to understand.

Perhaps if you did not know the order in which the letters of the alphabet come in.. But even if you wanted to explicitly define your own set, that is not the way to do it.

% continue doing this etc.. should have been your clue that you are doing something horribly wrong.

Turing:

const my_set : array 1..26 of string(1) := init('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')

loop
  put my_set(Rand.Int(1,26))
end loop
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Parker




PostPosted: Thu Dec 11, 2008 5:13 pm   Post subject: RE:How do you randomize letters?

Well, that confused me at first Tony, but I get it now after reading through it a bit.

The_Bean, the reason I did post that is because I didn't understand what you meant. I trusted it worked but wasn't sure if dc116 would understand it or not so I though I would give him another option.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: