473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Math.random() algorithm

About Math.random(), ECMA 262 just says "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 is not good enough for my purposes (to generate identifiers with
sufficient entropy, say 256 bits or so, to effectively preclude any chance
of any two calls ever pricking the same), but it may be a starting point.

Does anyone have information about the Math.random() function in various
implementations ?

More generally, is there any reliable way of picking up good noise, except
by setting up a server and calling it, say, with XMLHTTRequest?

--
Johannes
"Quand on dit c'est un Johannes, cela vaut autant que ce que maintenant
on appelle un pédant" (H. Estienne, in Littré, art. PÉDANT)
Sep 4 '07 #1
34 4100
Johannes Baagoe wrote:
About Math.random(), ECMA 262 just says "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 is not good enough for my purposes (to generate identifiers with
sufficient entropy, say 256 bits or so, to effectively preclude any chance
of any two calls ever pricking the same), but it may be a starting point.

Does anyone have information about the Math.random() function in various
implementations ?
http://www.merlyn.demon.co.uk/js-randm.htm should keep you busy for a
while.
More generally, is there any reliable way of picking up good noise, except
by setting up a server and calling it, say, with XMLHTTRequest?
var unique_url = 'file.php?' + (new Date()).getTime ();

Hope this helps,

--
Bart

Sep 4 '07 #2
Bart Van der Donck :
Johannes Baagoe :
>Does anyone have information about the Math.random() function in various
implementation s?
http://www.merlyn.demon.co.uk/js-randm.htm should keep you busy for a
while.
About half an hour, since the interesting stuff appears to be off-line.

But if the parallel with Borland Pascal/Delphi is valid, the algorithm
would be good old linear congruential Ã* la Lehmer, which is both a bit
surprising for floating point random functions and certainly not good
enough especially when seeded with something as predictable as time.
>More generally, is there any reliable way of picking up good noise,
except by setting up a server and calling it, say, with XMLHTTRequest?
var unique_url = 'file.php?' + (new Date()).getTime ();
That most certainly does not qualify as *good* noise, sorry...

http://www.random.org/integers/?num=...col=16&base=16
is more in line with what I want.
Hope this helps
It did, to a certain extent: ascertaining a negative is always useful.

--
Johannes
"Quand on dit c'est un Johannes, cela vaut autant que ce que maintenant
on appelle un pédant" (H. Estienne, cité dans le Littré, art. PÉDANT)
Sep 4 '07 #3
Johannes Baagoe wrote:
About Math.random(), ECMA 262 just says "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 is not good enough for my purposes (to generate identifiers with
sufficient entropy, say 256 bits or so, to effectively preclude any chance
of any two calls ever pricking the same), but it may be a starting point.

Does anyone have information about the Math.random() function in various
implementations ?

More generally, is there any reliable way of picking up good noise, except
by setting up a server and calling it, say, with XMLHTTRequest?
Hi Johannes,

Personally I WOULD go serverside if the 'quality' of the random numbers
is important for your current task.
You didn't tell us why you need 256 bits of entropy, but if you use
javascript you will stay OS/Browser dependent.
So you're stuck with finding out which OS/browser combi does what. Quite
a job.
Unless you find a nice trustworthy webpage that sums it all up. (I did a
quick search but could find one.)

I think you are probably better of using a good random package on the
server.
In case you need 1000 numbers, just let the server generate 1000 good
random numbers.
And get them in with a XMLHTTRequest.
Why do you want to avoid that solution (you gave yourself already)?

Or even use REAL random bytes, generated by nuclear radiation.
eg: http://www.fourmilab.ch/hotbits/how3.html

Not sure this helped. ;-)
Good luck.

Regards,
Erwin Moller
Sep 4 '07 #4
In comp.lang.javas cript message <GK************ *********@gigan ews.com>,
Tue, 4 Sep 2007 09:08:49, Johannes Baagoe <ba****@baagoe. orgposted:
>About Math.random(), ECMA 262 just says "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 is not good enough for my purposes (to generate identifiers with
sufficient entropy, say 256 bits or so, to effectively preclude any chance
of any two calls ever pricking the same), but it may be a starting point.
If you mean by 256 bits what I think you mean : ISO/IEC 16262 permits it
but does not require it. The chance of Math.random providing it (before
PCs have 256-bit integers) is IMHO negligible. It will not, likewise,
be crypto-grade.
>Does anyone have information about the Math.random() function in various
implementation s?
IE6 & IE7 appear to have 53 or more bits of randomness; FF 2.0.0.3 52
bits; Opera 9.21 31 bits. But think about the method used to test that.

One browser is reliably reported to have given 1.0 about once per 30000
calls to Math.random(). That suggests a 15- or 16-bit implementation,
with a bug.

The important questions are probably : what speed of generation do you
need, and have you access to Knuth's books.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Sep 4 '07 #5
In comp.lang.javas cript message <11************ *********@57g20 00hsv.goog
legroups.com>, Tue, 4 Sep 2007 07:15:09, Bart Van der Donck
<ba**@nijlen.co mposted:
>
>More generally, is there any reliable way of picking up good noise, except
by setting up a server and calling it, say, with XMLHTTRequest?

var unique_url = 'file.php?' + (new Date()).getTime ();
Round here, the UTC time is strictly monotonic.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demo n.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Sep 4 '07 #6
Johannes Baagoe said the following on 9/4/2007 9:27 PM:
Dr J R Stockton :
>If you mean by 256 bits what I think you mean : ISO/IEC 16262 permits it
but does not require it.

If anybody Out There has had the same idea of attempting to provide unique
ids for absolutely anything that one might ever want to identify, I would
certainly join efforts rather than reinvent the wheel, but I am not aware
of anybody else being quite that crazy :-)
It happens every day with Bar Codes :)

The only way to ensure that an ID is not duplicated (in your scenario)
is to keep track of the ones assigned and not re-assign them.
>One browser is reliably reported to have given 1.0 about once per 30000
calls to Math.random(). That suggests a 15- or 16-bit implementation,
with a bug.

Ouch! Which browser is that? It would mean that there would be a fair
chance of duplicates with only a few hundred elements.
Opera 6 would return 1.0 occasionally.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 5 '07 #7
Randy Webb :
The only way to ensure that an ID is not duplicated (in your scenario)
is to keep track of the ones assigned and not re-assign them.
Of course, but that means a global repository somewhere, with someone to
maintain it, control it and possibly charge outrageous rates once people
get hooked on the scheme. Sufficiently large random numbers mean that
anybody can make her own with a quite negligible probability of anybody
else ever picking the same.

--
Johannes
"Quand on dit c'est un Johannes, cela vaut autant que ce que maintenant
on appelle un pédant" (H. Estienne, cité dans le Littré, art. PÉDANT)
Sep 5 '07 #8
Johannes Baagoe said the following on 9/4/2007 10:01 PM:
Randy Webb :
>The only way to ensure that an ID is not duplicated (in your scenario)
is to keep track of the ones assigned and not re-assign them.

Of course, but that means a global repository somewhere, with someone to
maintain it, control it and possibly charge outrageous rates once people
get hooked on the scheme. Sufficiently large random numbers mean that
anybody can make her own with a quite negligible probability of anybody
else ever picking the same.
Simply generate a 16 digit number by picking 16 random numbers from 0-9
using Math.random() and be done with it. Don't make it any harder or
more difficult than it has to be :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 5 '07 #9
Randy Webb :
Simply generate a 16 digit number by picking 16 random numbers from 0-9
using Math.random() and be done with it.
Is there a good reason to believe that anybody who does that will always
come up with a different number from anybody else? Or nearly always, the
probability of exceptions being demonstrably so small that you would trust
a system which relies on your solution to operate, e.g., a nuclear plant?

My main contract just now is not quite as critical as that. However, it
concerns a hospital for children with quite severe diseases, and if,
say, a prescription goes to the wrong patient, someone could die.
Don't make it any harder or more difficult than it has to be
I quite agree that solutions should be as simple as possible to do the
job. But no simpler...

--
Johannes
"Quand on dit c'est un Johannes, cela vaut autant que ce que maintenant
on appelle un pédant" (H. Estienne, cité dans le Littré, art. PÉDANT)
Sep 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6115
by: Murat Tasan | last post by:
here is the situation... i have an array... and i select something from random from it. i pick a random number x by using Math.random() and multiplying it by the length of the array. but this gives me a double, not an int. so when i go to use the array, it needs an int. in the language spec, it tells me that round-towards-zero is used when going from floating point to integer, and the casting is done automatically if possible.
4
5030
by: Robert Scheer | last post by:
Hi. Reading about the Math.random method I saw that by default it generates between 0 and 1. To generate numbers between a greater range I can use these syntaxes: x = Math.random()/10 x = Math.random()*10 What is the difference between the two? I could not understand the
23
4173
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you initialize it with the same parameter. Can this be done with JavaScript? I couldn't find anything in the documentation. Basically, what I want to achieve is to obtain always the same sequence of random numbers for the same given initialization...
6
8764
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between Math.ceil/floor and parseInt +/- 1 to ensure the amount to move was at least 1 and in the right direction. I made a small test to see which one is faster and also included simply adding/subtracting 1. parseInt generally took 50% to 100% longer than...
3
17702
by: Bart Vandewoestyne | last post by:
Simple question: does the Java standard specify what random number generator algorithm should be used for Math.random() ? If 'no', then can anybody tell me what PRNG is behind Sun's version of Math.random()? Thanks, Bart -- "Share what you know. Learn what you don't."
21
13501
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
9
5602
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need to complete this part to move onto the next. <HTML> <HEAD> <TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.