0

I need to write a code with javascript which increase the given number and sum it with a random range numbers and finally displays it. This has to be saved, because the next day, this should increment current result, using the same "sum with random range of numbers".

for instance:

Given number: 135

Random range: 5 to 20

So:

Current day=135+random range, then keep it for next 24 hours.

Next day=Current day+random range, then keep it for next 24 hours....

but I don't know how to do it, would you please help me? I really appreciate it.

Mehdi
  • 13
  • 1
  • 11
  • And how do you wanna store this data? Cookie, Database... ? –  Dec 13 '11 at 11:19
  • Where you want store the actual number? localStorage, cookies or server-side solution? – Gabriel Gartz Dec 13 '11 at 11:19
  • An alternative to using storage is to use a random number generator that accepts a seed, which means you'll just run through n iterations where n is the current day. Examples: http://stackoverflow.com/questions/424292/how-to-create-my-own-javascript-random-number-generator-that-i-can-also-set-the – NibblyPig Dec 13 '11 at 11:36
  • Can I store it server-side? i'd like it to be shown to all users, I mean same result to be everyone – Mehdi Dec 13 '11 at 22:03

3 Answers3

0

If your aim is a client-side application and you don't have to support old browser I would suggest to use local-storage. With local-storage you can save your value. The next day you open the browser with the JavaScript-App and it will update your number. Instead starting the app manually you can use e.g. the timeout function.

Simon
  • 371
  • 2
  • 11
0

An function to get a number like this is:

function RandVal(minVal, maxVal)
{
    var randVal = Math.round(minVal+(Math.random()*(maxVal-minVal)));
}

So if you want to use local storage(cookies) you add to function:

document.cookie = "randVal=" + randVal;

For further information: W3Schools JS Cookies

But if you want to store on an database in a wab server, you have to use ajax:

W3Schools Ajax Tutorial

But it don't makes sense to use JavaScript to do this if you'll use a server-side application (e.g. PHP, JSP, ASP etc...) indeed. It will make more sense to do everything on server-side, in my oppinion.

0

Also, you could use your own simple pseudo-random generator, which would calculate current day value depending on the initial numbers (maybe stored in cookies) and a current date. It could save space, because cookies are very strict in size.

kan
  • 28,279
  • 7
  • 71
  • 101