I have written a random number generator using srand()
, 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 rand()%1000000
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() {
srand(time(NULL));
int size;
int* x;
ifstream myfile("size.txt");
ofstream outfile("input.txt");
while (myfile>>size) {
x=rng(size);
if (outfile.is_open()) {
for(int count = 0; count < size; count ++) {
outfile<< x[count] << " " ;
}
myfile.close();
}
}
return 0;
delete [] x;
x = NULL;
}