Anyone can pls help me with a unique random number generator of length 3, prefixed with values from DDMMYYYY (e.g. for today's date 25062015001)?

Recommended Answers

All 5 Replies

return Math.random() * 899 + 100;

Have a look at this one:

new Date().getFullYear()

Am sure you'll be able to figure it out.

Back to the person who wrote this spec. They didn't reveal how many they needed in a day so with 0 to 999 possible values, not only is there that "unique" spec but the missing spec on how to handle it running out of numbers.

Frankly the random bit could be tossed on most apps that need this and use sequential 0 to 999 then bomb out when there is no more to hand out. You can google generating a sequence of unique random numbers to find prior works.

I've been told that they won't be having much traffic on the site, so went forward with the 3-digit random number. Also, with the DDMMYYYY value added as prefix I think it should be good for use

That's fine lordrt and shows why so much code is buggy. Bad specs. Go with the sequence or off the web solution but at least you know it's designed to fail. That is, we know that robots and other bad things will hammer this and they'll be back to you to fix it again.

The code to get a random unique was easy to find on the web. Now take that and share your implementaion. I'll share that we went with sequence everytime. Even a 1000 position array of "random" for the sequence would do here.

So my code looks like this (am coding for Drupal here)

        var fullDate = new Date();
        var dd = fullDate.getDate();
        var mm = fullDate.getMonth()+1;
        var yyyy = fullDate.getFullYear();
        var today = dd+""+mm+""+yyyy;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.