I am trying to generate a unique random string.
var string = Math.floor( Math.random() * 1000 ) + Date.now();
The above method which I am using right now gives me too long a string and I want a shorter unique one.
I am trying to generate a unique random string.
var string = Math.floor( Math.random() * 1000 ) + Date.now();
The above method which I am using right now gives me too long a string and I want a shorter unique one.
Here is a workaround to avoid the issue of repeated ids:
var GenRandom = {
Stored: [],
Job: function(){
var newId = Date.now().toString().substr(6); // or use any method that you want to achieve this string
if( this.Check(newId) ){
this.Job();
}
this.Stored.push(newId);
return newId; // or store it in sql database or whatever you want
},
Check: function(id){
for( var i = 0; i < this.Stored.length; i++ ){
if( this.Stored[i] == id ) return true;
}
return false;
}
};
Based on Tushar's suggestion, (new Date().getTime()+'').substr(6,7)
will give you a 7-digits unique number. Not random, but unique... at least for a certain time. Since it's based on timestamp, it is actually milliseconds time. Since the length is 7 digits, it will loop from 0000000 to 9999999 every 1000 seconds, that's roughly 17 minutes. But you can't expect a higher "level of uniqueness" with only 7 digits. It may be enough for your application, depending on what you want to achieve with that.
Edit
Following up with your comment : you want a unique SECOND number, so millisecond is no use to you (should have mentioned the seconds in your question).
You could give an invoice the number of the current second, which is (new Date().getTime()+'').substr(3,7)
. That would be perfect, if the unique number was not limited to 7 digits. This makes 9.999.999 possible seconds, which is... around 115 days.
With 9 digits, you could print unique invoices numbers every second for 316 years.
So in my opinion, your only solution is just to start counting invoices from 0000001, then 0000002, etc, and keeping track of that somewhere to ensure uniqueness. I don't get how generating a random number between 0 and 9.999.999 will make invoices numbering more efficient or easier to track.
For an invoice number you should guarantee a unique identifier no matter how long. In which case I'll suggest using an UUID/GUID. See http://guid.us/GUID/PHP for two implementations (windows-COM/non-windows). If you are not familiar with the concept of UUID, check wikipedia here: http://en.wikipedia.org/wiki/Universally_unique_identifier .
Another option for you is to use mysql's native uuid generator:
select uuid();
This you can also use in your insert queries, like:
insert into mytable (id, data) values (uuid(), 'mytext');
Of course your table structure needs to be adjusted. Personally I find it best to use traditional integer auto_inc id as primary key, along with a guid field to use in replication/export/etc as key.
EDIT: There apparently is also a uuid_short() function in mysql (since 5.1.20).
SELECT UUID_SHORT();
92395783831158784
See https://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_uuid-short