Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generating a number once a day for all users in JavaScript

I’m relatively new to JavaScript, and I’m looking to create something like a Wordle clone. Like Wordle, I want to generate a new word each day for all users. I’ve seen solutions with a random number generator that stores the number in localstorage. However, wouldn’t this mean each user who goes on the website would have a different number? Is there a simple way to generate the same number for all users on a site?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can make your own pseudo random generator, these aren’t actually random but they generate numbers based on a seed (which will be the date)

// Get the day of the month with Date object
const day = new Date().getDate();

// And the month to prevent repeats
const month = new Date().getMonth();

Then you can create your function.

To make it seem even more random, you can get the middle numbers and use those.

function random(){
   // Crazy math stuff
   let num = Math.round((day+4) / month * 39163).toString();

   // To convert it back to a number, use the + operator before parentheses
   // Don’t forget to use % on the max value, I just put 31 as a placeholder
   return +(num[2] + num[3]) % 31;
}

This can be changed to fit your needs ^

This will be the same for every user for the entire day since it is pseudo random and based on a seed

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading