# rand() to choose?

• 03-26-2002
wagman
rand() to choose?
If I have a function like select(int a, int b, int c) how can I use the rand() to select a, b or c and then return that value? ie.. if a=30, b=40 and c=50 how can i use rand() to just pick b and return 40 back out? And then the next time i call the function, if c=45 use rand() to pick c that time and return 45?

So, in other words, I only need rand() to randomly pick either a, b or c and then return it. I know that the faq shows how to use rand to generate an actual value but thats not what I need for this. Thanks for any help.
• 03-26-2002
RobR
Generate a random number between 1 & 3, and then use a select case to select the relevant function based on the value.

(Alternatively, use a GOTO [J/K Prelude]).
• 03-27-2002
Skeptic
Sone thing like this will work but to get a more random number you should really use a time seed "srand(time()0;" under the stdlib.h and time.h headers

.code

int select(int a, int b, int c)
{
int temp=rand()%3+1;
if(temp==1)
return a;
if(temp==2)
return b;
if(temp==3)
return c;
}