How to pick two numbers

random(-4,4); i want to pick either -4 or 4 not inbetween


  • edited March 2018

    int rnd = random(1) < .5? -4 : 4;
  • if(random(100) < 50)

    int rnd=-4;

    else rnd=4;

  • In your case, conceptually, some of the many options:

    1. you can imagine multiplying 4 by a random (1 or -1),

    2. more generally you can imagine testing a random (0-1) to see if it is above or below .5, then choose one of two numbers accordingly.

    3. You could create a list of numbers and then choose one by shuffling.

    The last approach is much more useful if you needed more than one choice and you need them to be unique, but it conceptually works like the sides of a coin or deck of cards: if you shuffle (4, -4) and then take the top number, it is one or the other.

Sign In or Register to comment.