Seeding random number with Jscript?

123 views
Skip to first unread message

Moloney, Ciaran

unread,
Nov 26, 2007, 12:02:04 PM11/26/07
to X...@softimage.com

Hi,
I'm trying to seed randomness in my scripts, using the Jscript Math.random() method.
I can't for the life of me figure out how to seed this random number generator. Some resources suggest placing a seed value in the parenthesis, but even with a constant, the returned value is always random (seeded by time?).

Any tips on how to overcome this would be greatly appreciated.

Thanks,
Ciaran Moloney

Robert Moodie

unread,
Nov 26, 2007, 12:22:00 PM11/26/07
to X...@softimage.com
This will give you a random number (integer) between 1 & 1000
You could add to it by creating a PSet with an upper and lower range and
piping the values from the PSet into it.

var randomnumber=Math.floor(Math.random()*(1000 - 1) + 1)
Application.LogMessage ( "randomnumber = " + randomnumber )


Math.floor gives better results than Math.round to create the ints


________________________________

Thanks,
Ciaran Moloney


---
Unsubscribe? Mail Majo...@Softimage.COM with the following text in body:
unsubscribe xsi

Moloney, Ciaran

unread,
Nov 26, 2007, 12:36:48 PM11/26/07
to X...@softimage.com

Sorry, I guess I didn't make my question clear.
You method works well for purely random numbers, but I'm looking for repeatability.
I want to use a seed so that every time I call the random function, I will get the same pseudo-random result, resulting from that seed value.

Cheers.

Robert Moodie

unread,
Nov 26, 2007, 12:44:05 PM11/26/07
to X...@softimage.com
yeah, I see what you mean.
 
Not sure JScript will do that for you. Here's what the Windows Script docs say:
 
"The pseudorandom number generated is from 0 (inclusive) to 1 (exclusive), that is, the returned number can be zero, but it will always be less than one. The random number generator is seeded automatically when JScript is first loaded."
 
 


From: Moloney, Ciaran [mailto:cmol...@nybg.org]
Sent: November 26, 2007 12:37 PM
To: X...@Softimage.COM
Subject: Re: Seeding random number with Jscript?

David Barosin

unread,
Nov 26, 2007, 2:15:50 PM11/26/07
to X...@softimage.com
Maybe try other functions like sin,cos or modulus to fake a random
number. This way you can feed the function a frame number or object
index to generate a repeatable result.

Kent Bowling

unread,
Nov 26, 2007, 2:29:41 PM11/26/07
to X...@softimage.com
I've googled a little for JScript random gnerator seeding, but I haven't seen a good answer yet.
 
Maybe you could put together a very thin COM wrapper around the C library functions and call that from JScript?
 
-Kent

Halfdan Ingvarsson

unread,
Nov 26, 2007, 2:43:15 PM11/26/07
to X...@softimage.com
Here's a fairly decent one based on info from http://crypto.mat.sbg.ac.at/results/karl/server/node4.html :
 
function Random( seed )
{
 this.seed = seed;
 this.Next = function( )
  {
   this.seed = ( 0x7FF8A3ED * this.seed + 0x2AA01D31 ) % 4294967296;
   return( this.seed / 4294967295 );
  }
}
 
var ran = new Random( 19 );
LogMessage( ran.Next() );
LogMessage( ran.Next() );
LogMessage( ran.Next() );
LogMessage( ran.Next() );
 
The hex/dec constants may look a bit arbitrary but are specifically chosen this way due to the way JScript deals with floats/ints.
 
 - ½

From: owne...@Softimage.COM [mailto:owne...@Softimage.COM] On Behalf Of Kent Bowling
Sent: 26-Nov-2007 14:30

Moloney, Ciaran

unread,
Nov 26, 2007, 3:13:20 PM11/26/07
to X...@softimage.com

Thankyou, Sir!


---------- Forwarded message ----------
From: "Halfdan Ingvarsson" <hingv...@Softimage.COM>
Date: Nov 26, 2:43 pm
Subject: Seeding random number with Jscript?
To: XSI Mailing List Archive


Here's a fairly decent one based on info fromhttp://crypto.mat.sbg.ac.at/results/karl/server/node4.html:

kim aldis

unread,
Nov 27, 2007, 12:12:08 PM11/27/07
to X...@softimage.com

There is no seed function for jscript’s RNG. A good RNG is a useful thing to have in your toolbox. I’d grab Halfy’s and keep it. (fnaar).

kim aldis

unread,
Nov 27, 2007, 12:12:49 PM11/27/07
to X...@softimage.com

Beware the overhead of calling commands, especially custom commands.

 

From: owne...@Softimage.COM [mailto:owne...@Softimage.COM] On Behalf Of Kent Bowling


Sent: 26 November 2007 19:30

Reply all
Reply to author
Forward
0 new messages