T
Toby Newman
I need to randomly choose one of four paths in my program. Using the
tools I know, the best way I can think to do it is by doing something
like the following:
//==============================
#include <stdlib.h> //allow rand() function
int i;
i = rand (); // i = any number of the set [0,2^32 -1] or [0, 4294967295]
if (0<=i<(4294967295/4))
{ /* choice one*/ }
else if (4294967295/4<=i<(4294967295/2))
{ /* choice two*/ }
else if (4294967295/2<=i<3*(4294967295/4))
{ /* choice three*/ }
else if (3*4294967295<=i<=4294967295)
{ /* choice four*/ }
//==============================
This seems like a lot of work for a simple task. Is there some function
along similar lines to:
rand(3);
Which would generate a random integer of the set [0,3] ?
tools I know, the best way I can think to do it is by doing something
like the following:
//==============================
#include <stdlib.h> //allow rand() function
int i;
i = rand (); // i = any number of the set [0,2^32 -1] or [0, 4294967295]
if (0<=i<(4294967295/4))
{ /* choice one*/ }
else if (4294967295/4<=i<(4294967295/2))
{ /* choice two*/ }
else if (4294967295/2<=i<3*(4294967295/4))
{ /* choice three*/ }
else if (3*4294967295<=i<=4294967295)
{ /* choice four*/ }
//==============================
This seems like a lot of work for a simple task. Is there some function
along similar lines to:
rand(3);
Which would generate a random integer of the set [0,3] ?