I want to generate a random number with range from 1 to 40.
it will be used in either php or html page.
i just showing random image to the user by using his/her name.
if the two or more users with "same name" then the same image will be shown to the users who have same name. i.e., same image for all the users with same name.
for an example, consider there are 3 users with name 'david' and 2 users with with name 'sandy'. in this condition users name with david will be use same 3 images and users name with 'sandy' will be use another random image.
any suggestions...
You could map people with random images:
var users={};
function getImage(name){
return (users[name]=users[name]||Math.random()*40);
}
So you can do:
getImage("john");//e.g.5
getImage("mike");//6
getImage("john");//5
You can better use Math class to generate random numbers. Find below the sample code snippet
Math.floor(Math.random() * 40);
Bind the returned value to the specific name in a Map. If you want to do it for all sessions you need to handle it on server side
Just append the user id with your image while saving. Eg. "John"+userId+".jpg".
Generating unique random numbers (integers) between 0 and 'x'
Thant link would help you. In Javascript Math.random() would return any value between 0 and 1. So if you want to generate a random value between max and min numbers use Math.round(Math.random() * (max - min) + min)