Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can you use the RND statement with letters? 1

Status
Not open for further replies.

fetalmind37

Programmer
Apr 27, 2002
6
US
any info on the RND statement by someone who knows more than the qbasic 4.5 help would be greatly appreitiated. like can you randomise letters and special characters. more help on this topic would also be great but esp that.
 
Characters are represented using ASCII -- American Standard Code for Information Interchange -- which assigns each possible character a number. There is an ASCII table somewhere in the help file, but a few ranges you may find useful are:
[tt]
48-57 : "0" to "9"
65-90 : "A" to "Z"
97-122 : "a" to "z"
[/tt]
You can create a character from one of these numbers using [tt]CHR$()[/tt], so all you need to do is figure out how to turn a number in the range [0,1) into a number in the integer range [65,90]. Consider that if you multiply a number in the range [0,1) by 26, you get a number in the range [0,26). If you add 65, you get a number in the range [65,91). And finally, if you truncate the decimals, you get a number in the range [65,90]. The exact calculation for this example is:
[tt]
INT(RND * 26 + 65)
[/tt]
I'm sure you can generalize this to creating random lower-case letters and making strings randomized between both lower-case and upper-case letters and such :) All you need to do is think about the ranges you want, and convert from [tt]RND[/tt]'s range [0,1).
 
RND only produces a number between 0 and 1.

To get a number between a range (Min - Max) you can use Number = INT(RND * (Max - Min + 1) + Min

If you look up the ASCII codes for the letters/characters you want you can get a range for that.

RANDOMIZE TIMER
Num = INT(RND * 94) + 33
PRINT CHR$(Num)

Will print the characters in the range 33 - 126 (! - ~)

If the ASCII characters you want are not part of a continuous range you can use a SELECT CASE or IF statements.
Suppose you want the characters A - Z and a - z. you can use this.

RANDOMIZE TIMER
Num = INT(RND * 53) + 65
PRINT Num;
IF Num >= 91 THEN Num = Num + 6
PRINT CHR$(Num)

65 is the minimum number you want to produce (ASCII of "A"). You want 52 numbers (2 * 26). So the maximum number is 65 + 52 = 117. 117 - 65 + 1 = 53

THe ASCII value of "Z" is 90, but the value of "a" is 97. If the number produced is 91 then it gets turned into 97 by the additon of the 6.

Ray
 
We were answering the question at the same time.

The code does not tell him how to do what he wants to do. In fact ive no idea what letters and / characters they want to use.

The code I supplied can be applied to any range of the ASCII table, I just showed and gave hime the ASCII codes for the characters for A - Z and a -z. You gave them more than that by including 0 - 9.

Number = INT(RND * (Max - Min + 1) + Min is common code, which nearly all programmers know. I don't feel I gave away any secrets giving this to anyone. In fact it's on my webpage.
 
thanks to both of you, i think i can figure out what i need to do from there.
 
Just in case you don't have a ASCII list there is a ASCII quick-refernce in Help>Contents.
 
ok thanks

one of my friends told me to use the chr$ in tandem with rnd and if thens, so i should be fine, thanks to all of you who replied
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top