473,396 Members | 1,676 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,396 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 1970
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.