Skip to main content
Tomas Sinkunas
Legend
April 25, 2014
Answered

Random Seed

  • April 25, 2014
  • 1 reply
  • 1054 views

Hi guys.

Is there any logical way how to implement Random Seed?

Curently I am using Math.random(), bus I'd like to have ability to control seed for that random. As far as I know random generator is based on current time in JavaSript (am I correct?) and theres no straight forward way to have a seed for that.

Any ideas?

Thank you.

This topic has been closed for replies.
Correct answer chriskelley

Hi Tomas -

You are correct that Math.random() uses the current time.  Unfortunately there isn't a built-in method for generating a random seed in JS.

However, you could implement your own (ugh, no thanks) or you could import a library such as this: https://github.com/davidbau/seedrandom/blob/master/seedrandom.js into your script to give yourself access to such a method.

If you download that script, you can import using:

#include 'path/to/lib/seedrandom.js'

See the commenting in that file for proper usage, but basically it will prototype a Math.seedrandom('foo') method for you.

1 reply

chriskelleyCorrect answer
Participant
April 25, 2014

Hi Tomas -

You are correct that Math.random() uses the current time.  Unfortunately there isn't a built-in method for generating a random seed in JS.

However, you could implement your own (ugh, no thanks) or you could import a library such as this: https://github.com/davidbau/seedrandom/blob/master/seedrandom.js into your script to give yourself access to such a method.

If you download that script, you can import using:

#include 'path/to/lib/seedrandom.js'

See the commenting in that file for proper usage, but basically it will prototype a Math.seedrandom('foo') method for you.

Tomas Sinkunas
Legend
April 30, 2014

Sorry for late reply.

Yes, thank you. This might be a way to import a libraby. Thanks for your help.