get a random number

how can I return a number between 2 with a probability of 0,5?
for example I have 3 numbers : 1,2,3 . if i choose 1 my programme has to return 2 or 3. My code works for choice==1 and choice==3 but I dont't know how to do it for choice==2.
1
2
3
4
5
  if(choice==1)
            return randint(2,3);
        else
            if(choice==2)
               
        else
            if(choice==3)
                return randint(1,2);


my fonction randint :

int randint (int a, int b) {
return rand()%(b+1-a)+a;
}
Last edited on
Two logically distinct operations.

First, you want to get either true or false, with equal chance. See:
http://www.cplusplus.com/reference/random/bernoulli_distribution/

Now you have a random result. The second step is to use result with "real numbers". For example:
1
2
3
4
5
6
7
8
9
10
11
12
int fubar( int choice, bool result ) {
  switch ( choice ) {
  case 1:
    return result ? 2 : 3;
  case 2:
    return result ? 1 : 3;
  case 3:
    return result ? 1 : 2;
  default:
    return 0;
  }
}
Topic archived. No new replies allowed.