How do you randomize letters?
Author |
Message |
dc116
|
Posted: 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
|
|
|
The_Bean
|
Posted: 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.
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
|
Posted: 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 |
|
|
|
|
|
The_Bean
|
Posted: 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
|
Posted: 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
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Parker
|
Posted: 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. |
|
|
|
|
|
|
|