8

What changes adding the parameter in Math.random()?

For example:

Math.random() == Math.random(1234)
user2428118
  • 7,935
  • 4
  • 45
  • 72
asifg
  • 411
  • 2
  • 5
  • 5

6 Answers6

13

Math.random doesn't take params.

If you want to generate a random number between 2 intervals (a and b) you can use the formula:

math.random()*(b-a)+a
CristiC
  • 22,068
  • 12
  • 57
  • 89
  • See also [Make JavaScript Math.random() useful](http://www.shawnolson.net/a/789/make_javascript_mathrandom_useful.html). – user2428118 Oct 09 '12 at 13:16
  • why +a at the end? I don't understand why that is important. Entering Math.floor(Math.random() * (100-1)) will give you a number between 1 and 100... – john_h Jan 10 '18 at 20:16
9

Read the specification:

15.8.2.14 random ( )

Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209
5

Nothing. There is no seed for Math.random in Javascript. Anything inside the function call will just be dropped.

kapa
  • 77,694
  • 21
  • 158
  • 175
John Green
  • 13,241
  • 3
  • 29
  • 51
4

No official parameters. Have a look here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random

The confusion is understandable. Several sites have a seed in the function since it came from C / Java. More information about this ignored parameter here: Math.random() - Not random

If you want a better random number get one from here http://www.random.org/clients/http/ - you will need to wrap it in some server based client - see here for more information Cross domain ajax request from javascript file without help of server side code

UPDATE: Emailed the creator of random.org - he replied he is working on a jsonp implementation...

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
3

It will just ignore the passed parameter.

Amareswar
  • 2,048
  • 1
  • 20
  • 36
2

It doesn't - Math.random() doesn't take any parameters... :)

chrisfrancis27
  • 4,516
  • 1
  • 24
  • 32
  • and how can i make a stronger random or other random? – asifg May 17 '11 at 09:16
  • 2
    A "stronger random"? I'm afraid you've lost me... If you mean random between a different range, check [Parkyprg](http://stackoverflow.com/users/347807/parkyprg)'s answer. – chrisfrancis27 May 17 '11 at 09:22