[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Random Numbers

How can you program C++ to output random numbers? Note : these numbers should be different everytime the program is run
0
manchester_united
Asked:
manchester_united
  • 5
  • 2
  • 2
  • +3
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now