include include include include using namespace std int main

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()

{

//unsigned seed = time(0);
   //srand(seed);
   srand(static_cast<unsigned long>(time(0)));
   long y;
   ofstream OutputFile;
   OutputFile.open(\"1000Ints.txt\");
   for (int i = 0; i < 100000; i++)
   {
       y = (rand() % (1000001)) + 0;
       OutputFile << y << endl;
   }

}

I need to generate 100000 integer values (in the range of 0 to 1000000), but when I used rand(), I cannot generate all numbers. The largest number I can generate is 30000 and something.

Solution

Hi If you need any help please inform

=================================================================


This value we get using rand() function is library-dependent, Highest value a rand function return is
32767, so you are getting all values in between 0 and 30000.

and you need values in between 0 and 1000000

Now 32767*30 is 983010

If we multiply 3267 with 300 it will be like 9830100

Now 9830100%1000001=830091 (i.e you got a 6 digit numer)

So what you can do for 5000 number you can only choose

y = (rand() % (1000001)) + 0;

but for remaining one you can choose like

y=((rand()*300)%(1000001)) +0;

============================

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//unsigned seed = time(0);
//srand(seed);
srand(static_cast<unsigned long>(time(0)));
long y;
ofstream OutputFile;
OutputFile.open(\"1000Ints.txt\");

for (int i = 0; i < 50000; i++)
{
y = (rand() % (1000001)) + 0;
OutputFile << y << endl;
}
for (int i = 0; i < 50000; i++)
{
y=((rand()*300)%(1000001)) +0;
OutputFile << y << endl;
}
}

==========================================

output will be like

16977
22256
6463
6463
11812
13240
3424
23470
20389
17672
26426

----

-------

986997
21695
947899
683498
883194
640993
274200
449694
432894
343996
229491

#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; int main() { //unsigned seed = time(0);
#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; int main() { //unsigned seed = time(0);

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site