Randomization Issues

Recommended Posts

Hey Guys...

Wanted to know if there is a way to perform randomization to single numbers.

I'll explain.

`\$Res=Random(1,100)`

This will perform a randomization between 1 to 100.

What should I do in order to perform a randomization between 323, 560, 23, etc?

10x

Share on other sites
• Developers

You means randomly pick a value from your list?

```\$i="323, 560, 23,40"
\$ir = StringSplit(\$i,",")
msgbox(4096,"Predefined Random","Your number is:" & \$ir[round(Random(.5,ubound(\$ir)-1)+.5)])```
Edited by JdeB

Live for the present,
Dream of the future,
Learn from the past.

10x JdeB,

What is this .5?

Share on other sites
• Developers

10x JdeB,

What is this .5?

Good that you ask... since I made a mistake in the logic...(see below update

here how it works:

- ubound(\$ir) returns 5 in this case so need to subtract 1

- Random(.5, 4 + .49)-> will result in a random number between .5 - 4.49

- the round function will round:

0.5 - 1.49 -> 1

1.5 - 2.49 -> 2

2.5 - 3.49 -> 3

3.5 - 4.49 -> 4

- so \$ir[\$rn] retruns one of the array values.

Does that make sense ?

```\$i="323, 560, 23,40"
\$ir = StringSplit(\$i,",")
\$rn = Random(.5,ubound(\$ir)-1 + .49)
\$rn = Round(\$rn)
\$rn = \$ir[\$rn]
msgbox(4096,"Predefined Random","You number is:" & \$rn)```

Live for the present,
Dream of the future,
Learn from the past.

Share on other sites

Great! That's exactly what I needed!

10x again JdeB

Share on other sites

It is easier on the user and the system to use Int.

```\$i="323, 560, 23,40"
\$ir = StringSplit(\$i,",");\$ir[0] contains the number of strings extracted.
; Argument of Random is the number of values to choose from. (4)
; The 1 being added is the starting valid value.
\$rn = Int(Random(\$ir[0]))+1
\$rn = \$ir[\$rn]
MsgBox(4096, "Predefined Random", "You number is:" & \$rn)```

This is my prefered method of using Random; from the guy who wrote the random function.

Yeah, there is a bug in the two-argument version for Random , but the 1 and 0 argument versions are working fine.

Edit: I spoke (wrote?) too soon. Jon has installed the fix for Random in the current unstable version. Thanks, Jon.

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share on other sites
• Developers

It is easier on the user and the system to use Int.

`\$rn = Int(Random(\$ir[0]))+1`
good to know this also works ..... make it easier to code.. Edited by JdeB

Live for the present,
Dream of the future,
Learn from the past.

Create an account

Register a new account