-2

I'm in the process of coding an application that does the following:

  1. Generates a random number with 4 digits.
  2. Changes it once per calendar day.
  3. Won't change that full day. Only once in a day.

I tried:

function my_doubt()
 {
   var place = document.getElementById("my_div")
   place.innerHTML=Math.floor((Math.random()*100)+1);
 }

I'm getting a random number with Math.random(). However, I'm rather clueless about how to generate a different number for each day. What are some common approaches for tackling this problem?

Note: It doesn't have to be really random. A pseudo - random number is also OK.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
KarSho
  • 5,699
  • 13
  • 45
  • 78
  • 3
    Have you tried anything? Do you have any example code? –  Sep 27 '13 at 05:38
  • Of course it's possible. – elclanrs Sep 27 '13 at 05:38
  • Why do this client-side, when a server-side solution (using any server-side language) would be more robust, and easier to implement? – David Thomas Sep 27 '13 at 05:40
  • Do you need all installations to get the same number for the same day? Do you need no two installations to get the same number? – Thilo Sep 27 '13 at 05:42
  • 1
    I want a Ferrari F40. 1. It must be yellow 2... Thank you :)) – cbayram Sep 27 '13 at 06:12
  • @cbayram I just asked possible or not? tell yes or no. I dont ask F... sry, Ferreri like you... – KarSho Sep 27 '13 at 06:41
  • @cbayram 3 people answerd me. They are not fools... – KarSho Sep 27 '13 at 06:41
  • 2
    KarSho: the reason for *my* down-vote, and close-vote, were because you've shown no attempts to solve the problem yourself, you've not sufficiently outlined your requirements (specifically you've not bothered to answer Thilo's question here in the comments, despite having seen it), you 'faked' code (perhaps accidentally) by formatting your requirements *as* code (they're really not), and took the time to present a shopping list, rather than a programming problem. – David Thomas Sep 27 '13 at 06:42
  • @DavidThomas thanks for reply. I tried to slove. I created code for random and all. But i don't know is there any way or not. So only i asked. I didn't ask code. I asked is possible or not. That's it... No one know all things.... right? – KarSho Sep 27 '13 at 06:43
  • Then your question was answered, in the second comment, by [@elclanrs](http://stackoverflow.com/questions/19043377/random-number-in-javascript-per-day-once#comment28142591_19043377). – David Thomas Sep 27 '13 at 06:47
  • yes mr.Thomas. So, no mistake in my question. Then why down-vote and close-vote? – KarSho Sep 27 '13 at 06:49
  • It's a terrible question, for all the reasons I outlined above. Having received an answer does not excuse that, and a simple 'yes'/'no' question serves little-to-no purpose, the lack of code means it has no use to future visitors except, perhaps, as encouragement, it has no learning or educational value whatsoever. Albeit that's in my own opinion, others are free to disagree and try and post pseudo-code answers. But, again, I feel they're trying to discern your question which wasn't specific, and features no demonstrated attempt. – David Thomas Sep 27 '13 at 06:52
  • No, let's not continue this in chat: I've said all I have to say on this matter. – David Thomas Sep 27 '13 at 06:55
  • k, i tried for 1 full day to fix this. But I can't. Then only i posted here. I got one clear way. If any one have this question in future, this question/answers gives one idea. They no need to waste time like me... – KarSho Sep 27 '13 at 06:58

4 Answers4

4

You need to seed the random number generator with a number derived from the current date, for example "20130927" for today.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

You haven't been clear about your requirements, so I don't know how random you need (do you have requirements for how uniform of a distribution you need?).

This will generate a random looking 4 digit number which may be good enough for your requirements, but if you perform an analysis you'll find the number isn't actually very random:

function rand_from_seed(x, iterations){
  iterations = iterations || 100;
  for(var i = 0; i < iterations; i++)
    x = (x ^ (x << 1) ^ (x >> 1)) % 10000;
  return x;
}

var random = rand_from_seed(~~((new Date)/86400000)); // Seed with the epoch day.
Paul
  • 139,544
  • 27
  • 275
  • 264
1

Now that your question is a bit more reasonable, clear and nicer in tone. I can give you a way to get the same result on the client-side. However as others mentioned, to maintain consistency, you probably want to maintain the number on the server to ensure consistency.

var oneDayInMs = 1000*60*60*24;
var currentTimeInMs = new Date().getTime();  // UTC time
var timeInDays = Math.floor(currentTimeInMs / oneDayInMs);
var numberForToday = timeInDays % 9999;
console.log(numberForToday);
// zero-filling of numbers less than four digits might be optional for you
// zero-filled value will be a string to maintain its leading 0s
var fourDigitNumber = numberForToday.toString();
while(fourDigitNumber.length < 4)
{
  fourDigitNumber = 0+fourDigitNumber;
}
console.log(fourDigitNumber);

// remember that this number rotates every and is unique for 10000 days
cbayram
  • 2,259
  • 11
  • 9
0

1)create a random number in javascript

2)store in cookie that will expire after one day

3)get value from cookie, if it does not exist goto 1

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64