3
Contributors
5
Replies
16
Views
2 Years
Discussion Span
Last Post by Ritesh_4
1
return Math.random() * 899 + 100;

Have a look at this one:

new Date().getFullYear()

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

0

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.

0

@rproffitt
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

Edited by Ritesh_4

0

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.

0

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');
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.