Jump to content

Skipping Numbers in ASCII

Recommended Posts

In the autoit help file under Random there is an example for a random letter. I don't seem to understand this for some reason...

```;Random letter
If Random() < 0.5 Then
;Capitals
\$Letter = Chr(Random(Asc("A"), Asc("Z"), 1))
Else
;Lower case
\$Letter = Chr(Random(Asc("a"), Asc("z"), 1))
Endif```

I dont get what the \$Letter is doing, tried to look it up in the index and found nothing.

Also I dont understand the < 0.5

And confused on the Chr and Asc. I know thats like the hole script but Im new and im just trying to learn ...

Like if someone could basically just explain what everything in this script and what it does for the script that would be sweet =)

Share this post

Share on other sites

Random without parameters will return a float number .. the number will be less than 1. So the code is saying if Random() returns a number less than 0.5 make capital letters, otherwise, make lower case.

http://www.autoitscript.com/autoit3/docs/appendix/ascii.htm

Chr() converts a decimal number zero to 255 to it's ASCII character value. So Chr(65) would be "A".

Asc() converts an ascii character value to a decimal, so Asc("A") would equal 65.

Rewritten:

```Local \$s_letter = ""
Local \$n_number = Random()
If \$n_number < 0.5 Then
\$s_letter = Chr(Random(65, 90, 1)); "A - Z", random
Else
\$s_letter = Chr(Random(97, 122, 1)); "a - z", random
EndIf

MsgBox(64, "Info", "\$n_number = " & \$n_number & @CRLF & "\$s_letter = " & \$s_letter)```

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post

Share on other sites

At first I was like o.- what the then I tried the code you said, makes so much more sense. Thanks so much man!

Share this post

Share on other sites

How do I skip numbers in this? Like i only want Caps and non caps no symbols

```Local \$s_letter = ""
\$s_letter = Chr(Random(65, 122, 1)); "A - Z", random
MsgBox(64, "Info", @CRLF & "\$s_letter = " & \$s_letter)```

anyone know how?

Share this post

Share on other sites

I dont want any symbols, its prolly impossible but giving it a try on here. Like just Cap letters and noncaps.

```Local \$s_letter = ""
\$s_letter = Chr(Random(65, 122, 1)); "A - Z", random
MsgBox(64, "Info", @CRLF & "\$s_letter = " & \$s_letter)```

Share this post

Share on other sites

If I understand what your asking this may help...

```\$s_letter = _GenerateLetter()
MsgBox(64, "Info", @CRLF & "\$s_letter = " & \$s_letter)

Func _GenerateLetter()
Local \$Uletter
Local \$Lletter
\$Uletter = Chr(Random(65, 90, 1)); "A - Z", random
\$Lletter = Chr(Random(97, 122, 1)); "a - z", random
If Random(0, 1, 1) Then
Return \$Uletter
Else
Return \$Lletter
EndIf
EndFunc```

Uppercase letters are in ASCII 65-90 and Lowercase are in 97-122. the random you had inluded 91-96 which are symbols.

Share this post

Share on other sites

Please follow the forum rules on posting.

Don't bump posts within 24 hours of your last post, and don't post duplicate topics on the same subject.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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

×

• Wiki

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.