Sign in to follow this  
Followers 0
DebraMcEntire

Picking a random letter

6 posts in this topic

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 this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

#5 ·  Posted (edited)

 

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

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

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)

userbar.png

Share this post


Link to post
Share on other sites

Thank you all very much .

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0