Need Help with simple rand
I''m seem to be a bit stupid here so can someone please help
i need to write a single statement to print a number at random from the following set of number
1.
2 4 6 8 10
then again
2.
3 5 7 9 11
3.
6 10 14 18 22
you are only allowed to use a single statement.. no arrays or anything
Cheers
I''m assuming you''re using C here as never implied otherwise.
1. (rand () % 5 + 1) * 2)
Get a number from 1 to 5 then multiply by 2
2. ((rand () % 5 + 1) * 2) + 1)
Get a number from 1 to 5, multiply by 2, add 1
3. (6 + (4 * (rand () % 5)))
Add 4 times a random number from 0 to 4 to 6
I hope you can understand it. I used so many brackets just to keep the math clear.
-Goku
1. (rand () % 5 + 1) * 2)
Get a number from 1 to 5 then multiply by 2
2. ((rand () % 5 + 1) * 2) + 1)
Get a number from 1 to 5, multiply by 2, add 1
3. (6 + (4 * (rand () % 5)))
Add 4 times a random number from 0 to 4 to 6
I hope you can understand it. I used so many brackets just to keep the math clear.
-Goku
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement