JRS: In article <lr********************************@4ax.com>, dated
Wed, 16 Aug 2006 09:46:13 remote, seen in news:comp.lang.javascript,
tamsun <ta****@gmail.composted :
>Great, It's a good method. thank you very much.
I wonder can this method ensure I can create the unique string ?
I don't know the random mechanism of javascript,
e.g. I have 300 computers, and create this random
string concurring, javascript can ensure the
random string is unique?
On Mon, 14 Aug 2006 21:43:39 +0100, Dr John Stockton
<jr*@merlyn.demon.co.ukwrote:
Please read the newsgroup FAQ on proper article formatting.
Only collective management can ensure uniqueness.
Indeed, if your 300 computers are identical, and the function
Math.random() is called at the same time (on some definition of time) on
all of them, then you probably should get identical results on all 300.
(The method of initialisation of the Random Number Generator is
unspecified, but it is common to use clock time; time from some other
origin could be used. Note that the resolution of the initialisation
may be less than that of the generator - see for example <URL:
http://www
..merlyn.demon.co.uk/pas-rand.htm>.)
One can test the RNG to see its resolution : "Generator Properties" in
<URL:http://www.merlyn.demon.co.uk/js-randm.htm>.
It seems that some browsers have a 32-bit resolution, others at least
53-bit, in the RNG.
The question of probability of uniqueness is like that of the well-known
birthdays question; pick 23 people at random, and probably two or more
will have the same birthday.
With a 32-bit generator, ISTM that your chances of non-uniqueness are of
the order of 1 in (4E9/300^2) which is about 50000 - you will have to
decide whether that would be good enough, remembering Murphy.
Note : Evertjan's test does what it should; but, as Math.random() can
give exactly 0.5, it cannot be used as a direct substitute for mine.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.