Jump to content

Random number problems?


Recommended Posts

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?
Link to comment
Share on other sites

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

  • Developers

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

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...