cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Selecting one of two random numbers.
Selecting one of two random numbers.
Nov 18, 2008 at 5:06pm UTC
Sophisto
(11)
I need to select one of two random numbers in my program. I was originally going to put the two values in an array and srand to pick one, but as I'm only using two, I was wondering if there's a more efficient way of doing this.
Any hints/tips would be greatly appreciated.
Cheers,
S
Nov 18, 2008 at 5:38pm UTC
mahlerfive
(119)
Just wondering why this would ever be useful. If they are both random numbers, why not just generate one?
Nov 18, 2008 at 5:46pm UTC
Sophisto
(11)
Sorry. What I meant is I need to pick one of two numbers at random.
Nov 18, 2008 at 5:50pm UTC
Bazzy
(6281)
Get a new random number and if it is even, use the firt number; if it is odd, use the second number:
1
2
3
4
int
n1, n2, r; r=rand();
if
(r%2==0)
// use n1;
else
//use n2
Last edited on
Nov 18, 2008 at 5:51pm UTC
Nov 18, 2008 at 5:57pm UTC
Sophisto
(11)
Cheers, Baz. I'll give it a shot.
Topic archived. No new replies allowed.