I want to generate a random number that will be in the range of 1 to 6 (I know how to do that) but won't be equal to 4 other variables (for instance, their names are "num1", "num2", "num3" and "num4") in the algorithm. Can I do that? and how?
-
1Let me see if I understand... You want four variables, each with a random value between 1 and 6 (inclusive), but no repeats? – Fiddling Bits Jan 07 '20 at 18:44
-
You can do that. One solution would be to have two loops. Outer loop to set each variable with a random value; inner loop to check that random value is not a repeat. – Fiddling Bits Jan 07 '20 at 18:47
-
9Or shuffle the array `{1, 2, 3, 4, 5, 6}` and take the first four values. – M Oehm Jan 07 '20 at 18:48
-
is there any sophisticated way to check that the value is not repeated? that's the reason I asked if there is a way to add conditions... sadly, I cant shuffle an array, I cant submit things we havent learnt in class... – shubbbik Jan 07 '20 at 18:53
-
3@Oona why sophisticated? You could just check that your value does not equal to any other random values, and if they are equal, just generate another random number. – Ach113 Jan 07 '20 at 18:57
-
3I don't know what you've learned in class, but [shuffle isn't hard to implement](https://stackoverflow.com/q/6127503/10077). – Fred Larson Jan 07 '20 at 18:58
-
Isn't a simpler and less ambiguous way of specifying is "_five_ variables each with a unique value 1 to 6?" It is not clear in your question that the variables `num1` etc. are unique, but your response to @FiddlingBits question would suggest that. You need to fix the question rather then add important information in a comment response. in your question `num1` to `num4` could all have the same value so long as it was not the same as the value of your random value. – Clifford Jan 07 '20 at 19:44
-
Also the bit you say you know how to do; include that in your question so that an answer can build on it rather then conflict with it. – Clifford Jan 07 '20 at 19:44
-
If you want what @FiddlingBits has suggested (and you answered yes to that, even though that is not what your question states), then you need five unique integers in the range 1 to 6, so all you need is to select _one_ random integer from 1 to 6 to _omit_. – Clifford Jan 07 '20 at 19:47
-
With 4 variables each having values [1...6] without repeat leads to 6*5*4*3 or 360 combinations. Try generating a random number [0...359], divide by 6 to find first, then divided remainder by 5 to select one of 5 remaining digits, then to the next ...... – chux - Reinstate Monica Jan 08 '20 at 00:29
-
@chux-ReinstateMonica although he responded "yes" to Fiddling Bits clarification, it seems that 5 unique values are required rather than 4. The question is in dire need of clarification. – Clifford Jan 08 '20 at 08:19
1 Answers
It is not (currently) clear in the question, but from the clarification in the comments:
You want four variables, each with a random value between 1 and 6 (inclusive), but no repeats?
Assuming in fact you need 5 variables because the question states:
won't be equal to 4 other variables
then since you need 5 unique variables from 6 possibilities. The simplest solution then is to pick at random just one of the possibilities to omit. Furthermore the sensible means of storing 5 values of the same type is to use a single array variable rather then five separate variables - it makes the code far simpler.
int num[5] ;
int omit = 1 + rand() % 6 ;
for( int i = 0, j = 0; i < sizeof(num) / sizeof(*num); i++, j++ )
{
if( i != omit ) num[j] = i ;
}
Note that the expression 1 + rand() % 6
will have a bias for some values even if rand()
were entirely random. An exercise for the reader if that is an issue.

- 88,407
- 13
- 85
- 165
-
`1 + std::rand() / ((RAND_MAX + 1u) / 6)` can generate [1...7]. e.g. `rand() == 2147483647, RAND_MAX == 2147483647` --> 7. For such learner code, `1 + std::rand()%6` would suffice. – chux - Reinstate Monica Jan 08 '20 at 00:42
-
I cannot see how you get 7 from that (when correctly applying operator precedence). I agree that %6 would suffice, but was trying avoid comments from pedants regarding bias. That is why I asked the OP to include his method, so that the decision was his regarding its importance. – Clifford Jan 08 '20 at 08:13
-
Clever way to rethink it, randomly choosing numbers to omit rather than include. – Fiddling Bits Jan 08 '20 at 13:53
-
"how you get 7" : (2147483647 + 1u)/6 --> 357913941. 2147483647/357913941 --> 6. 1 + 6 --> 7. – chux - Reinstate Monica Jan 08 '20 at 14:28
-
@chux-ReinstateMonica : I knew you had to be right - more coffee needed. Rolled back to original text. If anyone is interested should have been `int omit ; do{ omit = 1 + std::rand() / ((RAND_MAX + 1u) / 6)} while( omit < 7 );` – Clifford Jan 08 '20 at 14:48
-
@Clifford Perhaps `while( omit < 7 )` --> `while (omit == 7)` (and more coffee)? – chux - Reinstate Monica Jan 08 '20 at 16:01
-