Hi
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)?

Dani AI

Generated

Quick summary and a safe client-side pattern: the original example (25062015001) expects a three-digit suffix that can include leading zeros (001). The Math.random variants shown earlier both miss that case and may exclude values (and they rely on the visitor’s clock). For a client-side generator that produces DDMMYYYY + a zero-padded 3-digit number (000–999), one simple and robust approach is:

const now = new Date();
const day = String(now.getDate()).padStart(2, '0');
const month = String(now.getMonth() + 1).padStart(2, '0');
const year = now.getFullYear();
const suffix = String(Math.floor(Math.random() * 1000)).padStart(3, '0');
const id = day + month + year + suffix;

Notes on correctness and durability: ’s random idea works for low-risk uses, but ’s point about uniqueness is important. Client-side generation cannot guarantee uniqueness across users or resists replay/robot attacks. For reliable, unique daily IDs generate the suffix server-side and persist it. A common MySQL pattern that atomically increments a per-day counter (so collisions are impossible) is:

INSERT INTO daily_counters (`day`, counter) VALUES ('YYYYMMDD', 1)
  ON DUPLICATE KEY UPDATE counter = LAST_INSERT_ID(counter + 1);
SELECT LAST_INSERT_ID() AS counter;

Create a UNIQUE/PRIMARY KEY on day so that the single-statement increment is atomic; then combine the returned counter (zero-pad to 3 digits) with DDMMYYYY.

Operational advice for (Drupal): implement the server-side generation in the form submit handler or a small API endpoint rather than only in client JavaScript, validate uniqueness before saving, and add a plan for exhaustion (1000 IDs/day) — either extend the suffix length, switch to a per-day sequential counter, or include a short timestamp/hash as fallback. Math.random is not cryptographically secure; use server sequencing for guaranteed uniqueness.

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)

drupal_add_js('jQuery(document).ready(function(){     
        var fullDate = new Date();
        var dd = fullDate.getDate();
        var mm = fullDate.getMonth()+1;
        var yyyy = fullDate.getFullYear();
        if(dd<10){
          dd="0"+dd
        } 
        if(mm<10){
          mm="0"+mm
        } 
        var today = dd+""+mm+""+yyyy;

        jQuery("#form-gen-id").val(today+Math.floor(Math.random()*899+100));});','inline');
Be a part of the DaniWeb community

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