2

I want to have 5 different random numbers but with the same number of digits generated.

I have this code:

int main()
{
  srand(time(0));
  unsigned int num1 = rand();
  cout << "random number 1 = " << num1 << endl;
  unsigned int num2 = rand();
  cout << "random number 2 = " << num2 << endl;
  unsigned int num3 = rand();
  cout << "random number 3 = " << num3 << endl;
  unsigned int num4 = rand();
  cout << "random number 4 = " << num4 << endl;
  unsigned int num5 = rand();
  cout << "random number 5 = " << num5 << endl;
}

The output is:

random number 1 = 278203697
random number 2 = 2102275865
random number 3 = 1018298572
random number 4 = 1658370388
random number 5 = 429634923

Meanwhile, my desired output is it generates the same amount of digits for all the numbers. For example:

random number 1 = 278203697
random number 2 = 210227586
random number 3 = 101829857
random number 4 = 165837038
random number 5 = 429634923

How to make it like the output that I want?

Thank you, I appreciate any help

gx_
  • 4,690
  • 24
  • 31
dulipat
  • 171
  • 2
  • 4
  • 12

3 Answers3

4

If you want the numbers to all contain the same number of digits, you either have to create a lot of random numbers. Or use some new functionality introduced in C++11, like the std::uniform_int_distribution class:

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(100000000, 999999999);

for (int n = 1; n <= 5; ++n)
    std::cout << "random number " << n << " = " << dis(gen) << '\n';

You can also just set the width of the output, and pad it with leading zeroes:

std::cout << std::setw(10) << std::setfill('0') << std::rand() << '\n';
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 6
    Without C++11, you don't need a lot of numbers; `1000 + rand() % 900` would do the trick (reducing the number of digits for clarity, and with the usual caveat that `%` introduces a small bias, and you'll need a couple of calls to `rand` if `RAND_MAX` is too small for the range). – Mike Seymour Aug 29 '13 at 11:22
  • 2
    @MikeSeymour Or you could use `rand() % 10` for each digit. (The bias will be smaller the smaller the modulo). Or possibly `rand() % 9 + 1` for the first digit. – James Kanze Aug 29 '13 at 11:30
  • @JamesKanze: You could indeed; although I'd call that a "lot" of numbers. – Mike Seymour Aug 29 '13 at 11:31
  • dividing is usually better than modulus: lower end bits are often more correlated than upper. – Yakk - Adam Nevraumont Aug 29 '13 at 11:59
-1

You can genertate a random integer number in the range from 100000000 to 999999999 (all 9 digit numbers) like this:

#include<cstdlib>

unsigned int num1 = rand()%900000000+100000000;

see also

In cstdlib RAND_MAX is 2147483647 on my system (Debian Squeeze g++ 4.4).

Community
  • 1
  • 1
cpp
  • 3,743
  • 3
  • 24
  • 38
  • 1
    you might want to check your logic, including counting zeros – ogni42 Aug 29 '13 at 11:39
  • 1
    @ogni42: C++ really needs [digit separators](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3499.html) to avoid that problem. Unfortunately, no-one can agree on what the separator should be. – Mike Seymour Aug 29 '13 at 13:58
-2

What you're looking for, then, is a number within the range 100,000,000 to 999,999,999 inclusive, or, equivalently, 100,000,000 plus a number between 0 and 899,999,999 inclusive:

unsigned int n = 100000000 + rand() % 900000000;

Please be aware of the usual caveats concerning modulo bias, though, if they matter to your application.

Community
  • 1
  • 1
Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
  • RAND_MAX likes to be like 32767 on some systems – Kos Aug 29 '13 at 11:23
  • @Kos and even if is larger, the modulo bias is likely to be significant with such large numbers. – James Kanze Aug 29 '13 at 11:31
  • And note that the top answer in the link in the answer gives a perfectly horrible solution to the modulo bias problem. – James Kanze Aug 29 '13 at 11:32
  • @MikeSeymour Agreed, but with the proposed solution, the modulo bias is likely to be very significant. And for the results he showed, 45 calls to `rand` is all that it would take, at one per digit. – James Kanze Aug 29 '13 at 11:36
  • @JamesKanze That's a well chosen usage of "perfectly horrible". ("Horribly perfect" might also work here.) – Kos Aug 29 '13 at 11:44
  • @JamesKanze: Good point, I didn't consider that the range was probably close to `RAND_MAX`. Two calls, assuming that gives you about 64 bits of randomness to play with, would probably be enough to make the bias negligible; but I agree that making nine calls as you propose is more straightforward. – Mike Seymour Aug 29 '13 at 11:44
  • @Kos True enough, though since the numbers he was getting in his example were large enough, it's not a concern for his particular system. You're right that he should be aware of it, though. – Cairnarvon Aug 29 '13 at 12:24
  • @Cairnarvon: Which number? 900,000,000 is the size of the range (although I appear to have dropped a zero when I wrote it); "about 64" was based on an assumption of `RAND_MAX` being about 2^32; and nine is the number of digits, and therefore the number of calls to `rand` if you use James' suggestion of one call per digit. – Mike Seymour Aug 29 '13 at 12:30
  • @MikeSeymour FWIW: I made a quick test using the corrected version of the above. With a million samples, g++ had 0 with the high digit 0 (naturally), around 140000 with each of 1, 2 and 3, 115000 with 4 and 93000 each with 5-9. And VC++ had 1000000 with the top digit 1, and none with any other. (I conclude that its `RAND_MAX` is less than 1000000.) The modulo bias is clearly visible, even with the g++ implementation. – James Kanze Aug 29 '13 at 13:27