473,216 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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 1964
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

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

Similar topics

2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
3
by: Rob | last post by:
He , I need to generate some random numbers, right now I am using: Random pk = new Random(); int a = pk.nextInt(540); and wondering if there is any other methods in java to generate random...
15
by: John Cassidy | last post by:
This has been driving me crazy. I've done basic C in school, but my education is mainly based on object oriented design theory where Java is our tool. For some reason, while helping a friend with a...
2
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
18
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point...
3
by: td0g03 | last post by:
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.