Sign in to follow this  
Followers 0
BlueScreen

Randomization Issues

8 posts in this topic

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? :whistle:

10x

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

10x JdeB,

What is this .5?

Share this post


Link to post
Share on other sites

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)

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Great! That's exactly what I needed! :whistle:

10x again JdeB B)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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 B) , but the 1 and 0 argument versions are working fine. :angry:

Edit: I spoke (wrote?) too soon. Jon has installed the fix for Random in the current unstable version. :whistle: 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 this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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