generating random numbers

how do i go about writing code that generates random numbers??
ryuturkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tilexCommented:
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
errorx666Commented:
#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
NosfedraCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pratik20Commented:
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
n_fortynineCommented:
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
billiousCommented:
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
ryuturkAuthor Commented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CleanupPingCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.