esra esra - 1 year ago 41
C++ Question

Why is the digit size of the random number generator limited?

I have written a random number generator using

, which creates an array of random numbers of given size. I would like my random numbers taking values up to 1000.000 and to get this, I've defined each entry of the array as
in the code below. The weird thing is that, the random values are all up to around 30.000 and the bigger random numbers such as 987.623 are not created i.e. the digit count of the random numbers are not more than 5.
Does anyone have any idea of why is this happening? Is there another way (function) that you can offer to get random numbers bigger than these ones?

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <vector>
using namespace std;

int * rng(int size) {
int* a = NULL;
a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = rand() % 1000000;
if (a[i] == 0) {
a[i] += 1;
for (int j = 0; j < size; j++) {
cout << a[j] << " ";
delete[] a;
a = NULL;
return a;

int main() {
int size;
int* x;
ifstream myfile("size.txt");
ofstream outfile("input.txt");
while (myfile>>size) {
if (outfile.is_open()) {
for(int count = 0; count < size; count ++) {
outfile<< x[count] << " " ;
return 0;
delete [] x;
x = NULL;

Answer Source

RAND_MAX on your machine is obviously close to or at the minimum permitted by the standard: 32767.

There are plenty of alternatives available which offer better periodicity. Mersenne Twister is one such good alternative and forms part of the C++11 standard.

Also note that the statements after your return statement are unreachable. Consider a


as the return type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download