-3

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...

Dinesh DiNu
  • 681
  • 5
  • 16
  • check this https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range – LZ041 Jun 16 '17 at 05:21
  • Please post some code for us to work with. – Zac Webb Jun 16 '17 at 05:22
  • Zac Webb, i need to create one random number for specific text. while re use with tath name, then generated number come without change. – Dinesh DiNu Jun 16 '17 at 05:23
  • @DineshDiNu I understand that. But we need some code you have already written/attempted to work with... – Zac Webb Jun 16 '17 at 05:26
  • i tried with min, max range with javascript. but its dynamically changed whenever page loads.. – Dinesh DiNu Jun 16 '17 at 05:28
  • Instead of creating random number create a number based on the username, so that same username will end up in same number inside your range – Sasikumar Jun 16 '17 at 05:42

4 Answers4

1

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

M.Navy
  • 155
  • 7
1

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
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Just append the user id with your image while saving. Eg. "John"+userId+".jpg".

  • images are static.. only 40 images are allowed.. how can i use it with id.. if i use it in php database then it wil easy for me.. :-( – Dinesh DiNu Jun 16 '17 at 05:32
0

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)

Naveen Kerati
  • 951
  • 3
  • 13
  • 29