468,456 Members | 1,719 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

Wanting a simple way to generate a random number

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
3 1702
toefraz
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
1,764 Expert 1GB
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
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.

Similar topics

2 posts views Thread by Laphan | last post: by
3 posts views Thread by Rob | last post: by
15 posts views Thread by John Cassidy | last post: by
12 posts views Thread by Jim Michaels | last post: by
18 posts views Thread by Bob Cummings | last post: by
20 posts views Thread by jjmillertime | last post: by
24 posts views Thread by pereges | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.