0

I have some Javascript that selects a random number between 1 and 10, and then loads one of two images into img.src = based on whether the random number chosen was above 5.

This is my code:

if ((Math.random() * 10) + 1 > 5) {
    img.src = '1.jpg';
} else {
    img.src = '2.jpg';
}

This works perfectly for 2 images, however I now want it to choose from 4 images.

I tried doing a nested if statement with the above code but I realised I was running multiple instances of Math.random, which defeated the purpose.

So then I tried to define a variable at the start using Math.random, and then wrote a nested If statement as follows:

var picnumber = Math.floor((Math.random() * 4) + 1);

if (picnumber = 1){
  img.src = '1.jpg';
} else {
  if (picnumber = 2){
    img.src = '2.jpg';
  } 
  else {
  if (picnumber = 3){
    img.src = '3.jpg';
  }
  else {
    img.src = '4.jpg';
  }
}}

I’ve run this code over 20 times and it keeps loading the same image, image “1.jpg”. I think I’m doing something very basic wrong here, how can I ensure that the var picnumber will get assigned a random number (either 1, 2, 3 or 4), and that the correct image will be attached to img.src? (I want each image to have a 25% chance of being chosen).
Thanks in advance for your time!

2
  • 4
    var num = Math.floor(Math.random() * 4 + 1); img.src = num + ".jpg";. Keep the parts separate; a lookup or if can be applied trivially as needed. In any case the "problem" has nothing to do with random but has to do with = (assignment) vs == (equality compare). Commented Apr 16, 2015 at 19:05
  • 1
    stackoverflow.com/questions/14369591/… Commented Apr 16, 2015 at 19:11

3 Answers 3

5

You're using the assign operator (=) instead of the comparison operator (==). Also, you can use the elseif and with that way you don't need to put a new if inside the else statement:

var picnumber = Math.floor((Math.random() * 4) + 1);

if (picnumber == 1){
  img.src = '1.jpg';
} else if (picnumber == 2) {
    img.src = '2.jpg';
} else if (picnumber == 3) {
    img.src = '3.jpg';
} else {
    img.src = '4.jpg';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah thanks @alejandro-bar :) that makes sense! Quick question, if I wanted to expand the number of pictures to say, 200, would this method result in a slow loading time? Or would the application only load one picture (the one that gets selected randomly?)
3

I'd recommend putting the pictures in an array.

You could then grab a random picture like this:

var pics= ['1.jpg', '2.jpg', '3.jpg', '4.jpg'],
    picnumber = Math.floor((Math.random() * pics.length));

img.src= pics[picnumber];

6 Comments

Thanks @rick-hitchcock If I expanded the number of pictures to 200 would putting them in an array like this make the application load slower than it would with only a few images? I'm wondering if the Javascript actually checks/pulls all possible images before one is chosen, or if the loading/initiation speed will be the same no matter how many pictures are in the array?
It would probably be faster to use the array method, because it could index directly into it without trying each if test until it found the current picnumber.
It would also save you a bunch of typing! : )
Note that only the image selected randomly will actually be downloaded, whether you use the array method or the if method.
ah awesome :) thanks. I was wondering would this method: var num = Math.floor(Math.random() * 4 + 1); img.src = num + ".jpg"; be even faster than an array?
|
0

try this:

var picnumber = Math.floor((Math.random() * 4) + 1);

if (picnumber == 1) {
  img.src = '1.jpg';
} else {
  if (picnumber == 2) {
    img.src = '2.jpg';
  } else {
    if (picnumber == 3) {
      img.src = '3.jpg';
    } else {
      img.src = '4.jpg';
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.