Jump to content

Randomization Issues


Recommended Posts

  • 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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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 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...

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...