3

I need a random number which I will be storing in the database per record and that (the generated random number)too should not repeat in future. So is Math.random() function is good for that?

Thanks in advance

KT B
  • 115
  • 2
  • 9
  • http://stackoverflow.com/questions/8012002/create-a-unique-number-with-javascript-time – James Donnelly Jun 13 '16 at 10:22
  • It is a random number with no promise of uniqueness. That said, it is chosing from a superset of 2^53 values, it may not repeat a number that easily. – gurvinder372 Jun 13 '16 at 10:24
  • Like James Donnely added: new Date().valueOf() + math.random() doesn't guarrante uniqueness but makes it pretty hard to be repeated – Borjante Jun 13 '16 at 10:26
  • That's true there is no surety that the random wont repeat, it will and to make it work properly you need to add some number with it. Try with that for while i.e. till you won't get repetition of number. And then you good to go with it. – frnt Jun 13 '16 at 10:31
  • Random is random-of course it could repeat. – Dave Newton Jun 13 '16 at 11:18

5 Answers5

8

Random is random and statistically it will repeat somewhere in the future. What you can do is combine something unique with a random part - for example use the unix timestamp with a random number.

function getRand(){
    return new Date().getTime().toString() + Math.floor(Math.random()*1000000);
}
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Well, *statistically* it likely *won't* repeat, but obviously that not good enough for a uid. – Dave Newton Jun 13 '16 at 11:21
  • @DaveNewton *somewhere in the future*.. I think that any non-zero chance on an infinite timescale converges to 100% (or simply is 100% if we use limits). The point was that it is unsafe to only use a random number to generate a UID. Combining it with unix time stamp significantly reduces the chance of generating a duplicate ID - it would have to be generated simultaneously and actually generate the same appended random number.. – Dropout Jun 13 '16 at 11:34
  • My point was that it isn't *likely* to repeat in the lifetime of the OPs system, and I explicitly stated that it wasn't a sufficient process for uid generation. There's no such thing as infinite timescale in a real system-but I agree it shouldn't be done by chance. – Dave Newton Jun 13 '16 at 13:21
5

Math.random() will not give you a unique number, in general it will not even give you a real random number.

You can try to use a datetime-based random algorythm, or go for a random number and then check if it's already in your database, but both approaches are not 100% save. There is pretty much only one way to ensure the number you store is unique, which is on database level.

Ninigi
  • 1,311
  • 12
  • 19
2

You can see here for GUID generator for unique keys for your DB. Also there you can find a lot of good information about random mechanisms

Create GUID / UUID in JavaScript?

Community
  • 1
  • 1
Drag13
  • 5,859
  • 1
  • 18
  • 42
  • Link-only answers aren't really a good SO fit, and if you're linking to an SO answer, you should mark the question as a dupe. – Dave Newton Jun 13 '16 at 11:19
  • It is not dupe because main question is not directly about GUID or such other things. But my answer can show another way of solving TS question and can bring a lot of relevant information for him and other readers. – Drag13 Jun 13 '16 at 11:22
  • It's an X-Y problem. Even if it wasn't, link-only answers are still not ok. – Dave Newton Jun 13 '16 at 11:25
2

time + increment + random:

  var newGuid = (function() {
    var guid = parseInt(Math.random() * 36);
    return function newGuid() {
      return Date.now().toString(36) + (guid++ % 36).toString(36) + Math.random().toString(36).slice(2, 4);
    };
  })();
zswang
  • 2,302
  • 3
  • 16
  • 13
0

If you look at the specs of math.random, you will see that is defined as quasi-random number generator, meaning, it is not REALLY random. Moreover, a real random number generator will for sure repeat a result somewhere along the line BUT, when this happens, the series following this repetition will not resemble the series that followed the first appearance of the number.

Now, you mentioned that you need to store this in a database. Why don't you use a SEQUENCE (in Oracle; other DBMS have different mechanisms for this)? This will warrant that any used number will NEVER be reused. Moreover, if you don't want to use numbers in a sequence, you can using the value of this sequence as the seed for a random number (or a hashing). This will give you uniqueness to quite many digits.

FDavidov
  • 3,505
  • 6
  • 23
  • 59