# Picking a random letter

## Recommended Posts

I know the is a cleaner way to do this.

```; pick a random letter A - E

#include <MsgBoxConstants.au3>

Example()

Func Example()
Local \$Ran = Random(1, 5, 1)
if \$ran = 1 then
\$ran = "A"
EndIf
if \$ran = 2 then
\$ran = "B"
EndIf
if \$ran = 3 then
\$ran = "C"
EndIf
if \$ran = 4 then
\$ran = "D"
EndIf
if \$ran = 5 then
\$ran = "E"
EndIf
MsgBox(\$MB_SYSTEMMODAL, "", "The letter is: " & \$Ran)
EndFunc   ;==>Example```
##### Share on other sites

Maybe this way?

```#include <MsgBoxConstants.au3>

Global \$aChars[26], \$i, \$sLetter, \$iChars
For \$i = 0 To 25
\$aChars[\$i] = Chr(65 + \$i)
Next

\$iChars = 10

For \$i = 1 To \$iChars
\$sLetter &= \$aChars[Random(0, UBound(\$aChars) - 1, 1)]
Next

MsgBox(\$MB_SYSTEMMODAL, "", "The letter is: " & \$sLetter)```

Or much shorter:

```#include <MsgBoxConstants.au3>
Global \$sLetter, \$iChars = 10

For \$i = 1 To \$iChars
\$sLetter &= Chr(Random(68, 94, 1)) ;A-Z
Next

MsgBox(\$MB_SYSTEMMODAL, "", "The letter is: " & \$sLetter)```

Br,

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

```; pick a random letter A - E

#include <MsgBoxConstants.au3>

Example()

Func Example()
Local \$Ran = Chr(Random(65, 69, 1))
MsgBox(\$MB_SYSTEMMODAL, "", "The letter is: " & \$Ran)
EndFunc   ;==>Example```

##### Share on other sites

```Example()

Func Example()
Local \$Ran = Random(1, 26, 1)
; http://www.asciitable.com/
MsgBox(0, "", "The letter is: " & Chr(\$Ran + 64))
EndFunc   ;==>Example```

##### Share on other sites

I know the is a cleaner way to do this.

If you are having trouble following the examples given, just look in the ASCII Characters section in the Help file's Appendix section.

Of importance, is the first two columns, inline with the description column.

Basically, you are working with an incremental number that represents a letter in alphabetical order - Uppercase characters first, then lowercase versions, etc. Using the Chr command, will turn a number into a character i.e. Chr(65) = A, Chr(66) = B, etc. Of course, that number can be stored in a variable.

It is much easier to work with numbers (as a variable), then convert the result to alphanumerical.

P.S. Welcome to the Forum!

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

##### Share on other sites

Thank you all very much .

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...