-3

I need to generate 10 characters(both letters and digits) unique codes to apply for system generated gift vouchers. currently, I'm using following code.But it contains 13 numbers.

var today = new Date();
Number(today);

Is there is any other way to create 10 digit unique values to apply system generated gift vouchers.It should never same at any time.Code should be generated like in _id in meteor collection.What is the best possible way to do this?

In meteor it generates an unique id for records.Like that I need to create a 10 digit unique value

Thusila Bandara
  • 285
  • 4
  • 22
  • Does your code give you unique number but the problem is to make it give you only 10? – ibrahim mahrir Feb 03 '17 at 11:17
  • Really? You created a date object and then constructed a Number? How is that going to give you a unique value?? – Alon Eitan Feb 03 '17 at 11:17
  • Show us your effort and code. – Hemal Feb 03 '17 at 11:17
  • 3
    should it be unique throughout "one session" or even across browser reloads? Should it be unique globally or per user? For what reason? ID in a database (which would be a bad bad idea)? In general the only way is to use some kind of sequence and once the sequence is exhausted ... well you have a problem. – newBee Feb 03 '17 at 11:19
  • @everyone to be honest the date time is a unique value because it changes everytimes [EDIT] Nvm he/she split it so it won't be that unique. – Jerome Feb 03 '17 at 11:20
  • I'm not sure it `var date1 = new Date(), date2 = new Date();` Will give you unique value on computers that can run this code in less than 1ms – Alon Eitan Feb 03 '17 at 11:24
  • yes you correct @Jerome – Thusila Bandara Feb 03 '17 at 11:24
  • Is there a way to create 10digit unique value using javascript – Thusila Bandara Feb 03 '17 at 11:26
  • 2
    http://stackoverflow.com/a/105074/2270492 might help – Hemal Feb 03 '17 at 11:30
  • @ThusilaBandara see [Meteor's Random package](http://docs.meteor.com/packages/random.html). If you need further help, you should probably open a new question, as your current one is already bloated with comments and answers out of your new context (i.e. Meteor). – ghybs Jun 06 '17 at 07:30

3 Answers3

1

First approach: (not safe but get you random numbers)

var cache = {};

function getValue() {
  var value = Math.floor(Math.random() * 10000000000); // get a random number between 0 and 10000000000
  
  value = ('000000000' + rand).slice(-10); // make it 10 digits long
  
  return cache[value] == undefined? cache[value] = value: getValue(); // return it if it not been returned before, or return another number otherwise
}

console.log(getValue());
console.log(getValue());
console.log(getValue());
console.log(getValue());
// ...

Note: that over time the process might be very slow and that the stack might be exceeded thus becoming an error.

Second approach: (the safe one)

var START_ID = 1; // The first ID

function getValue() {
  var value = START_ID++;
  value = ('000000000' + value).slice(-10);
  return value;
}

console.log(getValue());
console.log(getValue());
console.log(getValue());
console.log(getValue());
// ...

Note: If you want the values to not have leading 0s. Change this:

value = ('000000000' + value).slice(-10);

to:

value = '1' + ('00000000' + value).slice(-9);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
1

Here is very short and effective and secure solution generated for time value, which is definitely unique:

var unique = new Date().valueOf();
console.log('WITH 13 digits '+ unique);
console.log('WITH 10 digits ' +String(unique).substring(3, 13));
Nezir
  • 6,727
  • 12
  • 54
  • 78
0

You can create a custom javascript function for that as follow.

function Unique()
{
    var text = "";
    var randstring= "0123456789";

    for( var i=0; i < 10; i++ )
        text += randstring.charAt(Math.floor(Math.random() * randstring.length));

    return text;
}
Hemal
  • 3,682
  • 1
  • 23
  • 54