448,485 Members | 1,031 Online Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

# Wanting a simple way to generate a random number

 P: 2 I'm writing in C. I am trying to get a simple way to generate a random number between 0 and 1. I'm not sure of two things - how to limit a random number to 0 or 1 and how to reset it every time I hit the "Enter" key. I want to try to do something simple like a coin flip. Each time the person presses enter, a random number is generated and if it is 0 it's heads and if 1 it's tails. I'm fairly new at C and am not sure exactly what to do. To start, here's what I've done so far (trying to just get a random number). I do not know how to limit it to 0 and 1. This gives me the same number every time I run it. Thanks! #include int main() { int x = rand(); printf("%d",x); return 0; } Jan 13 '08 #1
3 Replies

 P: 19 I'm writing in C. I am trying to get a simple way to generate a random number between 0 and 1. I'm not sure of two things - how to limit a random number to 0 or 1 and how to reset it every time I hit the "Enter" key. I want to try to do something simple like a coin flip. Each time the person presses enter, a random number is generated and if it is 0 it's heads and if 1 it's tails. I'm fairly new at C and am not sure exactly what to do. To start, here's what I've done so far (trying to just get a random number). I do not know how to limit it to 0 and 1. This gives me the same number every time I run it. Thanks! #include int main() { int x = rand(); printf("%d",x); return 0; } First off, you need to seed your random number generator. A good way to do this is with your system clock, as the number it seeds with changes everytime the program runs. To do this you need the header and place Expand|Select|Wrap|Line Numbers srand(time(0)); before your rand() function in main. To address the issue of getting either a 0 or a 1, use the modulus operator: Expand|Select|Wrap|Line Numbers rand() % 2 Hope this helps. Jan 13 '08 #2

 Expert 100+ P: 1,764 I'm writing in C. I am trying to get a simple way to generate a random number between 0 and 1. I'm not sure of two things - how to limit a random number to 0 or 1 and how to reset it every time I hit the "Enter" key. I want to try to do something simple like a coin flip. Each time the person presses enter, a random number is generated and if it is 0 it's heads and if 1 it's tails. I'm fairly new at C and am not sure exactly what to do. To start, here's what I've done so far (trying to just get a random number). I do not know how to limit it to 0 and 1. This gives me the same number every time I run it. Thanks! Expand|Select|Wrap|Line Numbers #include    int main() {   int x = rand(); printf("%d",x);    return 0; } You need first to initialize the generator with srand function: e.g Expand|Select|Wrap|Line Numbers #include  srand((unsigned)time(/*adress of time_t variable,can be NULL*/)); and them call rand()%num,which will generate a number between 0 and num-1. Jan 13 '08 #3

 P: 2 thanks so much for the help! Jan 14 '08 #4 