admin管理员组文章数量:1327525
I found the question here:
Create GUID / UUID in JavaScript?
The answer provides the following JS:
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
Now, Some of this seems silly to me. Why so much repetition? I planned on using this to name file being uploaded to my server so that they didn't override each other. This doesn't look like it will always generate a unique number.
What is the above codes benefit over just naming the file math.random()
. It doesn't even change the seed.
Sorry, I've never worked with GUID / UUID ever and some of the code doesn't really make any sense to me...
CLARIFICATION
A lot of people aren't answering the question like I asked it. A lot of people are explaining that GUID isn't always unique, blah blah blah. That isn't what I'm asking. I'm asking, what was the point of using it over just math.random()
.
Joe seems to have given the best answer for me in the ments.
I found the question here:
Create GUID / UUID in JavaScript?
The answer provides the following JS:
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
Now, Some of this seems silly to me. Why so much repetition? I planned on using this to name file being uploaded to my server so that they didn't override each other. This doesn't look like it will always generate a unique number.
What is the above codes benefit over just naming the file math.random()
. It doesn't even change the seed.
Sorry, I've never worked with GUID / UUID ever and some of the code doesn't really make any sense to me...
CLARIFICATION
A lot of people aren't answering the question like I asked it. A lot of people are explaining that GUID isn't always unique, blah blah blah. That isn't what I'm asking. I'm asking, what was the point of using it over just math.random()
.
Joe seems to have given the best answer for me in the ments.
Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Oct 29, 2011 at 17:47 FreesnöwFreesnöw 32.2k31 gold badges94 silver badges140 bronze badges 13- 4 Even that answer says: "do you want actual GUIDs, or just random numbers that look like GUIDs?" Because those aren't real GUIDs. Also from the original thread: "There's no way to generate real GUIDs in Javascript, because they depend on properties of the local puter that browsers do not expose." so my question is: do you need a GUID? Or just a random filename? There's nothing magical about a GUID as a consumer, it is not how it looks, it's how it's generated. For a random filename, using clock ticks + a random number would be (at least as) effective... – Joe Commented Oct 29, 2011 at 17:51
- 7 Someone wanted a thing that looked like a GUID, but was easier to get. Why do people buy fake Rolexes? – ObscureRobot Commented Oct 29, 2011 at 17:56
- 3 There is no guarantee that a GUID is unique. A GUID is semi-unique. Even considering not-well-formatted GUID, there are only 2^128 possible GUID. Thanks to the Birthday Paradox, if you generate 2^64 of them you have like 50% of generating a duplicate. – xanatos Commented Oct 29, 2011 at 18:31
- 1 @xanatos: that is true, though 2^64 is eighteen quintillion and change... – Joe Commented Oct 29, 2011 at 19:16
- 1 @Joe Still it isn't "unique". You couldn't "sync" all the world on a single "unchecked" GUID. In the end a GUID isn't a Globally Unique ID. – xanatos Commented Oct 29, 2011 at 19:20
2 Answers
Reset to default 4Even that answer says: "do you want actual GUIDs, or just random numbers that look like GUIDs?" Because those aren't real GUIDs. Also from the original thread: "There's no way to generate real GUIDs in Javascript, because they depend on properties of the local puter that browsers do not expose." so my question is: do you need a GUID? Or just a random filename? There's nothing magical about a GUID as a consumer, it is not how it looks, it's how it's generated. For a random filename, using clock ticks + a random number would be (at least as) effective...
In your case, no reason. But if you have JS code that is talking to something that is expecting a GUID or something in that form, you would need to pass something of a similar format since you can't generate the real thing in pure JS.
To make an example, even .NET GUID aren't "tied" to a machine. .NET uses v4 UUID for Guid.NewGuid()
( Simple proof that GUID is not unique ) so 122 bits of randomness plus 6 fixed bits. By reading here Are GUID collisions possible? it seems SQL Server uses a full random number (but I don't have an SQL Server to check... checked... It seems to use the same format of v4. There is a "fixed" 4 and the next "block" always begins with 8, 9, A, B. So 122 bits of randomness).
So what problem do you have with this random-generator that gives 128 bits of randomness in a "know" format? Would you prefer to see a non-fixed-format 128 bit number? Wow! Very practical... Here... Take one 43438471087229589138546501885363994076
(it's a GUID converted to its numericvalue, base 10). Now, how would you like to save it do your favourite DB? VARCHAR
or NUMERIC
? Isn't it easier to pack it in a GUID?
As a sidenote... What is the advantage over math.random()
? Well... Over a single use of math.random()
it's quite clear :-) I don't know how many bits of randomness a single call of math.random
has, but at max it's 63 bits (a number in JS is 64 bits, but 1 bit is for the sign). And probably many many less.
本文标签: javascriptWhat makes this pseudoGUID generator better than mathrandom()Stack Overflow
版权声明:本文标题:javascript - What makes this pseudo-GUID generator better than math.random()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196698a2431211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论