
-----------------------------------
dc116
Wed Dec 10, 2008 7:40 pm

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.

-----------------------------------
The_Bean
Wed Dec 10, 2008 7:46 pm

Re: How do you randomize letters?
-----------------------------------
Well the letters A-Z are represented by the numbers 65-90 in the ASCII code.

chr(num:int)

Will get the character for the certain ASCII code.

Rand.Int (num1,num2:int)

Will produce a random number between, and including num1 and num2.

-----------------------------------
Parker
Thu Dec 11, 2008 8:33 am

Re: How do you randomize letters?
-----------------------------------
This way involves alot more code, but in my opinion is easier to understand. 



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
Thu Dec 11, 2008 1:08 pm

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
Thu Dec 11, 2008 1:38 pm

Re: How do you randomize letters?
-----------------------------------
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.


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


-----------------------------------
Parker
Thu Dec 11, 2008 5:13 pm

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.
