Solved

# generating random numbers

Posted on 2003-03-14
Medium Priority
229 Views
how do i go about writing code that generates random numbers??
0
Question by:ryuturk
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Expert Comment

ID: 8140188
here we go! you must include "time.h" header. This is very simple...

I'll show you one way to do it...

#include <time.h>           //required for srand
#include <conio.h>          //required for getch()
#include <stdio.h>          //required for getch()
#include <iostream.h>       //required for cout
#include <stdlib.h>         //required for rand
using namespace std;

int main(int argc, char** argv[]) {
srand(time(NULL));      //sorry I don't remember
int rand_val;           //random value
int max_val = 10;       //max value
rand_var = rand()%(max_var+1);//initiating randomisation
cout << rand_var << endl; //prints out random value
getch();                //waiting...
}
0

Expert Comment

ID: 8140481
#include <time.h>
#include <iostream.h>
#include <stdlib.h>
// this function will generate a random number within the range minvalue to maxvalue
int random(int minvalue,int maxvalue)
{
return (rand() % ((maxvalue + 1) - minvalue)) + minvalue;
};

int main(void)
{
int numbers,min,max,i=0;
srand((unsigned int)time(0)); // seed the random number one time, do not do this repeatedly (!)
cout << "How many random numbers to generate?";
cin >> numbers;
cout << "Enter minimum value>";
cin >> min;
cout << "Enter maximum value>";
cin >> max;
while(i++ < numbers)
{
cout << random(min, max) << endl;
};
return 0;
};
0

LVL 3

Expert Comment

ID: 8141900
As a refinement, you also could be generating double numbers, by adding a sin(time(NULL)) or any other mathematical function (log, sqrt etc) to the rand() results previously suggested.

If you still want integers, you can multiply the double value with a power of 10 and obtain an integer with the first digits of the resulted fraction.
0

Expert Comment

ID: 8142474
The main fucntion for generating randon numbers is random(). But the number geberated is influenced by a perticular number called "seed". So, here you should set the seed value. If you set the seed value based on time, the series of integers generated by "random()" will be different every time.

Regards,
Pratik.
0

LVL 4

Expert Comment

ID: 8142493
you can seed the number more than once, provided that you know your program won't be calling rand() repeatedly within one second. The later version with time() returns the time down to seconds but no more so if you seed it again in one second you'll get the same time and the same number(s), obviously. =)
0

LVL 7

Expert Comment

ID: 8142800
Do you want to use the random-number generator built into many languages, or do you want to build your own random-number generator?

...Bill
0

Author Comment

ID: 8146258

bill i wouldn't mind if u give me an idea on how to do both, cheers.
0

LVL 84

Accepted Solution

ozo earned 500 total points
ID: 8147309
0

Expert Comment

ID: 9447132
ryuturk:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, itâ€™s also â€¦
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to goâ€¦
Starting up a Project
###### Suggested Courses
Course of the Month9 days, 1 hour left to enroll