Link to home
Start Free TrialLog in
Avatar of ryuturk
ryuturk

asked on

generating random numbers

how do i go about writing code that generates random numbers??
Avatar of tilex
tilex

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...
}
#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;
};
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.
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.
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. =)
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
Avatar of ryuturk

ASKER


bill i wouldn't mind if u give me an idea on how to do both, cheers.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ryuturk:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.