Random Numbers

How can you program C++ to output random numbers? Note : these numbers should be different everytime the program is run
manchester_unitedAsked:
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.

KangaRooCommented:
Use
  randomize()
  rand()
  random(int)


=========================================================
#include <stdlib.h>
int rand(void);

Description

Random number generator.
rand uses a multiplicative congruential random number generator with period 2 to the 32nd power to return successive pseudorandom numbers in the range from 0 to RAND_MAX. The symbolic constant RAND_MAX is defined in stdlib.h.

Return Value

rand returns the generated pseudorandom number.

=========================================================
#include <stdlib.h>
#include <time.h>
void randomize(void);

Description

Initializes random number generator.
randomize initializes the random number generator with a random value.

=========================================================
#include <stdlib.h>
int random(int num);

Description

Random number generator.
random returns a random number between 0 and (num-1). random(num) is a macro defined in stdlib.h. Both num and the random number returned are integers.

Return Value

random returns a number between 0 and (num-1).
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
KangaRooCommented:
Note, Use randomize ONCE, before the first call to rand() or random()
0
manchester_unitedAuthor Commented:
Now I can generate random numders put I can't print the numbers on the screen. How do I print the random numbers on the screen.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

manchester_unitedAuthor Commented:
plus I need the random numbers to go from 1 to 13.
0
jasonclarkeCommented:
#include <stdlib.h>
#include <time.h>

#include <iostream>

using namespace std;

void main( void )
{
   srand( (unsigned)time( NULL ) );

   /* Display 10 numbers. */
   for(int i = 0;   i < 10;i++ )
      cout << rand()%13+1 << endl;
}
0
KangaRooCommented:
Mh, seems random(int) and randomize() are not ANSI :(
Like jason said, or:

#include <stdlib.h>
#include <iostream>
#include <time.h>

inline int random(int n) { return rand() % n; }
inline void randomize() { srand(time(0)); }

int main()
{
   randomize();
   for(int i = 0; i < 10; ++i)
      cout << 1 + random(12) << endl;
}
0
mbormannCommented:
this maybe irrelevant here but do visit
             http://www.math.keio.ac.jp/~matumoto/emt.html

             Mersenne Twister Home Page
             (A random number generator)
             Four times faster than rand()
             Of period 2**19937-1

I have used the Java version and it's really good

             What is Mersenne Twister?

             Mersenne Twister(MT) is a pseudorandom number generator
             developped by Makoto Matsumoto and Takuji Nishimura
             (alphabetical order) during 1996-1997. MT has the following merits:


             It is designed with consideration on the flaws of various existing
             generators.
             The algorithm is coded into a C source downloadable below.

             Far longer period and far higher order of equidistribution than any
             other implemented generators. (It is proved that the period is
             2^19937-1, and 623-dimensional equidistribution property is
             assured.)

             Fast generation. (Although it depends on the system, it is reported
             that MT is sometimes faster than the standard ANSI-C library in a
             system with pipeline and cache memory.)

             Efficient use of the memory. (The implemented C-code mt19937.c
             consumes only 624 words of working area.)
0
KangaRooCommented:
Nice to know :)
0
cwguesCommented:
To make a program give you different random numbers each time the program is executed you will need to set the SEED of the random number generator to the current time or date........


HTH.....
0
mbormannCommented:
in Java atleast the constructor looks like

public MersenneTwister(Date d)
{}

:-)
0
nortonxeCommented:
i used your code but these code should not different when ran out
a picture captured result
r-n.jpg
0
KangaRooCommented:
I don't quite understand your question. Is it that some numbers are repeated?
0
nortonxeCommented:
yes, it repeated and i do not want it
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
C++

From novice to tech pro — start learning today.