Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old August 14th, 2006, 08:25 AM
tamsun
Guest
 
Posts: n/a
Default how to create guid by javascript?


we need create a GUID in web page, just like:
{26C2E7C8-C689-D1D5-C452-58EC5A2F2A39}

Could anyone tell me how to use javascript
to create it without ActiveX object?
  #2  
Old August 14th, 2006, 09:15 AM
Georgi Naumov
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

The following gives a description of the algorithm:
http://www.webdav.org/specs/draft-le...s-guids-01.txt

tamsun написа:
Quote:
we need create a GUID in web page, just like:
{26C2E7C8-C689-D1D5-C452-58EC5A2F2A39}
>
Could anyone tell me how to use javascript
to create it without ActiveX object?
  #3  
Old August 14th, 2006, 11:35 PM
Dr John Stockton
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

JRS: In article <ht90e21co20emmlfg0havcmpvi59snv0ih@4ax.com>, dated
Mon, 14 Aug 2006 15:36:48 remote, seen in news:comp.lang.javascript,
tamsun <tamsun@gmail.composted :
Quote:
>
>we need create a GUID in web page, just like:
>{26C2E7C8-C689-D1D5-C452-58EC5A2F2A39}
>
>Could anyone tell me how to use javascript
>to create it without ActiveX object?
To make a random string of that form, you can note that it is made up of
four-hex-character units.

function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).subst ring(1) }

should generate such a unit of random value;

Ans =
(S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()).toUpperCase()

--
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.
  #4  
Old August 15th, 2006, 11:05 AM
tamsun
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

Thank you very much.


On 14 Aug 2006 01:17:50 -0700, "Georgi Naumov" <gonaumov@gmail.com>
wrote:
Quote:
>The following gives a description of the algorithm:
>http://www.webdav.org/specs/draft-le...s-guids-01.txt
>
  #5  
Old August 16th, 2006, 02:35 AM
tamsun
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

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
<jrs@merlyn.demon.co.ukwrote:
Quote:
>JRS: In article <ht90e21co20emmlfg0havcmpvi59snv0ih@4ax.com>, dated
>Mon, 14 Aug 2006 15:36:48 remote, seen in news:comp.lang.javascript,
>tamsun <tamsun@gmail.composted :
Quote:
>>
>>we need create a GUID in web page, just like:
>>{26C2E7C8-C689-D1D5-C452-58EC5A2F2A39}
>>
>>Could anyone tell me how to use javascript
>>to create it without ActiveX object?
>
>To make a random string of that form, you can note that it is made up of
>four-hex-character units.
>
>function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).subst ring(1) }
>
>should generate such a unit of random value;
>
>Ans =
(S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()).toUpperCase()
  #6  
Old August 16th, 2006, 08:35 AM
Evertjan.
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

tamsun wrote on 16 aug 2006 in comp.lang.javascript:
Quote:
and create this random
string concurring, javascript can ensure the
random string is unique?
Uniqueness does not exist in an limited string.
is 1 in a million enough for you?
[changed the string function to:
Math.random().toString(16).substring(2)]


<script type='text/javascript'>

var a,b,n=1;

a = Math.random().toString(16).substring(2);

while (a!=b && n<1e6) {
b=a;
a = Math.random().toString(16).substring(2);
n++;
};

document.write(a+' (value)<br>');
document.write(n+' (number of strings tested)<br>');

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
  #7  
Old August 16th, 2006, 12:45 PM
Michael Winter
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

On 16/08/2006 02:46, tamsun wrote:

[snip]
Quote:
e.g. I have 300 computers, and create this random
string concurring, javascript can ensure the
random string is unique?
No. To guarantee that, make a central authority (a single designated
machine) responsible for creating and tracking the generated identifiers.

Mike


When replying to this group, please refrain from top-posting.
  #8  
Old August 16th, 2006, 11:25 PM
Dr John Stockton
Guest
 
Posts: n/a
Default Re: how to create guid by javascript?

JRS: In article <lrt4e2deccqt0kgk63l7camm59ctodl8fp@4ax.com>, dated
Wed, 16 Aug 2006 09:46:13 remote, seen in news:comp.lang.javascript,
tamsun <tamsun@gmail.composted :
Quote:
>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
><jrs@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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.