![]() |
Sign Up FREE! |
Sign In |
Classroom Setup |
Common Core Alignment ![]() |
![]() |
#1 |
Guest
Posts: n/a
|
![]() i want to write c++ code, that selects the following number with certain probability. Number 1 with probability 65/100Number 2 with probability 35/100How do i write the code?
|
![]() |
#2 |
Guest
Posts: n/a
|
![]() N1 = 65/100n2 = 35/100
|
![]() |
#3 |
Guest
Posts: n/a
|
![]() I haven't written c++ in a long time, but it would go something like this (comments included).double x; // declare your variableint r = rand(99); // get a random integer 0-99if (r>34) x = 1; // 65/100 chanceelse x = 2; // 35/100 chanceReplace 1 and 2 with whatever the numbers are.Actually you might be able to shorten that using the ternary operator.double x = (rand(99) > 34) ? 1 : 2;Yeah that's a way more elegant solution.
|
Thread Tools | |
Display Modes | |
|
|