Sign in to follow this  
Followers 0
mooboo

Random number problems?

3 posts in this topic

I want to get a random number between 1 and 3. When I use this script...

$ran = Random ( 1,3 )

msgbox (0, "", "" & $ran & "")

I ALWAYS get a long decimal number. How can I get it to be full numbers only? And how come I never had this problem before?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Sorry... I figured it out... the script needs to look like this...

$ran = Random ( 1,3 ) ;Get random number

$ran2 = Round ($ran) ;Round number to whole

msgbox (0, "", "" & $ran & "") ;Output

Edited by mooboo

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

think this is a better way:

$ran = int(Random (0,3 )+1)
msgbox (0, "", "" & $ran & "")

Your code will generate a number between 1.00000 and 2.9999999

1 - 1.499999 is rounded to 1

1.5 - 2.499999 is rounded to 2

2.5 - 2.999999 is rounded to 3

so theres a higher likelyhood that the result is 2 .... ( if i understand it correctly :D )

EDIT: come to think about it .. this should also work properly:

$ran = int(Random (1,4 ))
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        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