Jump to content
Sign in to follow this  
Chimaera

Random Numbers within a range

Recommended Posts

Chimaera

Im fooling with some random numbers and i think im making hard work of it so im asking is there an easier way to do this?

I make some random numbers like this

$RandStart1 = Random (48, 57, 1) ;48-57
$RandStart2 = Random (65, 90, 1) ;65-90
$RandStart3 = Random (97, 122, 1) ;97-122
;====================================================
$RandMid1 = Random (48, 57, 1)
$RandMid2 = Random (65, 90, 1)
$RandMid3 = Random (97, 122, 1)
;====================================================
$RandFinish1 = Random (48, 57, 1)
$RandFinish2 = Random (65, 90, 1)
$RandFinish3 = Random (97, 122, 1)

Then i mix them up like this

For $i = 1 To Random(2, 4, 1)
$sText &= Chr(Random($RandFinish3, $RandStart3, 1))
$sText &= Chr(Random($RandMid1, $RandStart1, 1))
$sText &= Chr(Random($RandFinish2, $RandStart2, 1))
$sText &= Chr(Random($RandStart2, $RandMid2, 1))
$sText &= Chr(Random($RandStart1, $RandMid1, 1))
$sText &= Chr(Random($RandMid3, $RandFinish3, 1))
StringTrimLeft($sText, Random(13, 19))
Next

Its a bit long winded i feel

the 3 types have to be Ascii numbers 48-57 (1-0) / 65-90 (A-Z) / 97-122 (a-z)

i run this three times and add them together to create a 25 - 40 ish long set of numbers letters etc

I dont need military grade random or anything severe just a easier way.

Any suggestions?

Share this post


Link to post
Share on other sites
SadBunny

How about this?

$chars="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
$text=""
For $i = 1 To Random(1,10,1)
$text &= StringMid($chars, Random(1, StringLen($chars), 1), 1)
Next

ConsoleWrite($text & @CRLF)

Exit

/edit: fooked up the alphabet :-/

/edit: this algorithm would ofcourse lead to a lower occurance rate for digits than for letters. If you want multiple "types" of characters (digits vs letters for instance) to have the same chance of occuring, you could divide them into groups through an array:

Dim $chars[2]
$chars[0] = "1234567890"
$chars[1] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

$text=""
For $i = 1 To Random(1,10,1)
$typeOfChar = Random(0, UBound($chars)-1,1)
$text &= StringMid($chars[$typeOfChar], Random(1, StringLen($chars[$typeOfChar]), 1), 1)
Next

ConsoleWrite($text & @CRLF)

Exit

Just some ideas.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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  

×