Aug 22, 2014 at 8:03am UTC
How could I generate numbers from 000,000,000-000,999,999 in this format:
000000000
000000001
000000002
ETC.
I can easily do this without the zero's in front of the digits but the front digits are a must. Is there a certain variable that I can do math.h (cmath) ops on that supports this??
Last edited on Aug 22, 2014 at 8:21am UTC
Aug 22, 2014 at 10:03am UTC
You could do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream> //std::cout, std::endl
#include <iomanip> //std::setw, std::setfill, std::boolalpha
#include <ctime> //std::time
#include <cstdlib> //std::rand, std::srand
int main()
{
std::srand(static_cast <unsigned >(std::time(0)));
unsigned const min = 0u;
unsigned const max = 999999u;
unsigned const digits = 9;
bool again = true ;
std::cout << std::setfill('0' );
while (again)
{
unsigned result = rand() % (max - min + 1) + min;
std::cout << "Random number: " << result << std::endl;
std::cout << std::setw(4) << ',' ;
if (result > 999 && result < 1000000) //4-6 digits
{
std::cout << std::setw(3) << result / 1000;
result -= result / 1000 * 1000;
}
else
std::cout << std::setw(4);
std::cout << ',' << std::setw(3) << result << std::endl;
std::cin >> std::boolalpha >> again;
}
}
Or possibly streaming the number to a string and parsing it accordingly. I am confused on the 0's on the left :P
*edit added a condition to make sure the number is less than 6 digits (even though the prng does that already :P)
Last edited on Aug 22, 2014 at 10:05am UTC
Aug 22, 2014 at 7:48pm UTC
Oh yeah I guess I could have generated a bunch of random digits from 0-9 lol. Meh it was 3 am for me :P
Aug 22, 2014 at 8:05pm UTC
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <iomanip>
#include <cstdint>
int main()
{
for (std::uint32_t i = 0; i < 1000000000; ++i)
std::cout << std::setfill('0' ) << std::setw(9) << i << std::endl;
}
Edit: too many zeros.
Last edited on Aug 22, 2014 at 10:55pm UTC
Aug 22, 2014 at 9:22pm UTC
I think he wanted them to have comma's though after every 3 digits. Or I could have completely misread what he was trying to do.
Last edited on Aug 22, 2014 at 9:23pm UTC