Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

generating random numbers

Posted on 2003-03-14
10
Medium Priority
?
230 Views
Last Modified: 2010-04-17
how do i go about writing code that generates random numbers??
0
Comment
Question by:ryuturk
9 Comments
 

Expert Comment

by:tilex
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

by:errorx666
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

by:Nosfedra
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Expert Comment

by:pratik20
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

by:n_fortynine
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

by:billious
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

by:ryuturk
ID: 8146258

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

Accepted Solution

by:
ozo earned 500 total points
ID: 8147309
0
 

Expert Comment

by:CleanupPing
ID: 9447132
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Make the most of your online learning experience.
Starting up a Project
Screencast - Getting to Know the Pipeline

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question