By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
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 <stdio.h>

int main()
{

int x = rand();
printf("%d",x);
return 0;
}
Jan 13 '08 #1
Share this Question
Share on Google+
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 <stdio.h>

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 <ctime> header and place

Expand|Select|Wrap|Line Numbers
  1. 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
  1. rand() % 2
Hope this helps.
Jan 13 '08 #2

Savage
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
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. int x = rand();
  7. printf("%d",x);
  8.    return 0;
  9. }




You need first to initialize the generator with srand function:

e.g
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. 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

Post your reply

Sign in to post your reply or Sign up for a free account.