[Server] sys.rand

Discussion in 'Simulator Suggestions' started by TheUnknownOne, May 18, 2012.

Thread Status:
Not open for further replies.
  1. TheUnknownOne

    TheUnknownOne Member

    Joined:
    Mar 28, 2011
    Messages:
    988
    Likes Received:
    3
    Currently, sys.rand returns an int. Can this be changed to long so that higher random numbers can be possible using this function?
     
  2. Lamperi

    Lamperi I see what you did there

    Joined:
    Apr 25, 2010
    Messages:
    2,647
    Likes Received:
    11
    I propose removing as there is already Math.random() (which allows higher numbers since it is using doubles - you can get 15 digit precision with it).

    Javascript doesn't really even support long, so I suggest using Math.random()

    http://en.wikipedia.org/wiki/Double-precision_floating-point_format

    You can get precision up to 2^53:
    Code (text):
    1.  
    2. js> (Math.pow(2,53)+1)-Math.pow(2,53)
    3. 0
    4. js> (Math.pow(2,52)+1)-Math.pow(2,52)
    5. 1
    6.  
    I suggest using a function like this:
    Code (javascript):
    1.  
    2. /* input: lower, upper - numbers, preferably integers */
    3. /* output: a random number between bounds [lower, upper) */
    4. function rand(lower, upper) {
    5.     if (upper > Math.pow(2,53)) print("rand-Warning: losing precision");
    6.  
    7.     return Math.floor(Math.random() * (upper-lower)) + lower;
    8. }
    9.  
     
    Last edited: May 18, 2012
Thread Status:
Not open for further replies.