0

I am trying to make a C++ program for my Ebay business that is 15 digits long, but I want the first 5 digits to be the same.

Like ex: 152328476529876 PIN: 1000 152323123642345 PIN: 9433 152321254213432 PIN: 3222

I tried making a random number generator, but I cant get it to where the first 5 digits are the same, but the last 10 digits are the same. With a random pin.

#include <iostream>
#include <cstdlib> 
const int maximum_number = 9999999999;
const int minimum_number = 1;
unsigned int i;
const int maximum_pin = 999;
const int minimum_pin = 0;
unsigned int pin;
int main()
{

    // Print 100 random numbers
    for (int count = 0; count <= 1000; ++count)
    {
        const int new_number = (rand() % (maximum_number - minimum_number)) + maximum_number;
        const int new_pin = (rand() % (maximum_pin - minimum_pin)) + maximum_pin;
        std::cout << "15232" << new_number << " Pin : "<< new_pin << "\n";
    }

    return 0;
152321410094708 Pin : 1384

152321410073128 Pin : 1567 etc

The problem I am having is that the first 5 numbers are the same, which is how I want it, but then the 14100 remains the same and should be randomized as well, not just the last 5 digits...

Plus the pins only stay at 1000, they never go above 1999 etc.

Yann Droneaud
  • 5,277
  • 1
  • 23
  • 39
michael
  • 9
  • 2
  • Possible duplicate of [How to generate a random number in C++?](https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c) – Yann Droneaud Mar 15 '19 at 13:44
  • The best way to generate uniformly distributed random number in modern C++ is describe in the following comment https://stackoverflow.com/a/13445752/611560 – Yann Droneaud Mar 15 '19 at 13:45
  • Have a look at https://en.cppreference.com/w/cpp/numeric/random – Jesper Juhl Mar 15 '19 at 16:22

2 Answers2

1

Instead of using rand() and modulo operations, you should make use of the <random> header from C++11. Using Walter E. Brown's function toolkit from n3847, you could use:

#include <cstdint>
#include <iostream>
#include <iomanip>
#include <random>

const uint64_t maximum_number = 9999999999;
const uint64_t minimum_number = 1;
const uint64_t maximum_pin = 9999;
const uint64_t minimum_pin = 0;
const uint64_t prefix = 15232;

auto& global_urbg() {
    static std::default_random_engine u{};
    return u;
}

void randomize() {
    static std::random_device rd{};
    global_urbg().seed(rd());
}

uint64_t pick_a_number(uint64_t from, uint64_t thru) {
    static std::uniform_int_distribution<uint64_t> d{};
    using parm_t = decltype(d)::param_type;
    return d(global_urbg(), parm_t{from, thru});
}

int main() {
    randomize();
    for (int count = 0; count < 100; ++count) {
        const uint64_t new_number = pick_a_number(minimum_number, maximum_number);
        const uint64_t new_pin = pick_a_number(minimum_pin, maximum_pin);
        std::cout << prefix << std::setw(10) << std::setfill('0') << new_number
                  << " Pin : " << std::setw(4) << std::setfill('0') << new_pin << "\n";
    }

    return 0;
}

Sample output:

152325021252553 Pin : 1766
152327824160815 Pin : 7717
152321697896629 Pin : 3659
152320879192254 Pin : 8832
152325057425972 Pin : 2831
152323353939394 Pin : 1681
152324684928448 Pin : 9800
152328117866049 Pin : 4173
152326884989008 Pin : 4663
152320007759205 Pin : 7264
152327718061191 Pin : 3968
152322605655403 Pin : 8213
152324442390907 Pin : 7916
152322386351545 Pin : 4683
152321687805442 Pin : 3886
152328171047426 Pin : 6344
152327001747780 Pin : 2636
152325373164268 Pin : 3676
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
0

Your problem here is that generating a random number with rand() gives you a number between 0 and at least 32767 (depending on your platform, see here) and this number is not large enough. Another problem is that the datatype int does not have enough bits to store such a large number.

This is all very platform dependant but these are the minimal sizes:

  • int uses at least 16 bits which can store numbers from 0 to 65536 (if unsigned)
  • long uses at least 32 bits which can store numbers from 0 to 4294967296 (in unsigned)
  • long long uses at least 64 bits which can store numbers from 0 to 18446744073709551616 (if unsigned)

So you already have a problem with your max values since they do not fit inside an int. If you want to be sure how large your numbers can be you can use stdint.h and specify your ints with uint64, int32 and such.

What you want is 10 random digits. This can be achieved with something like this:

for (int count = 0; count < 1000; count++) {
    // leading number must not be 0
    long long randomNumber = (rand() % 9) + 1;

    for (int i = 0; i<9; i++) {
        randomNumber *= 10;
        randomNumber += rand() % 10;
    }
    std::cout << "15232" << new_number << "\n";
}

The Pin has a similar problem I think

Mr.Yellow
  • 692
  • 5
  • 15
  • 1
    That works better than mine! Thank you! the only issue I have is that some of the digits are only 8 long and not 10. – michael Mar 15 '19 at 13:14
  • Oh i forgot, if the leading number is 0 the number will be truncated -> 0034 -> 34. A simple solution would be to add a leading non zero number. But since 0 is not possible anymore there are a few less numbers. I added it above but if you want to have leading 0s you should use a string (char array). – Mr.Yellow Mar 15 '19 at 13:21
  • 2
    It's advised not to use modulo as it introduces a bias in the resulting distribution random number distribution, see this for example https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator – Yann Droneaud Mar 15 '19 at 13:52
  • Yes you are right! But since this is not security relevant and he need to check for duplicates anyway this should be just fine for this purpose. – Mr.Yellow Mar 15 '19 at 14:01
  • 1
    @YannDroneaud -- while that's theoretically true, in practice the bias is insignificant for small ranges. If `RAND_MAX` is 32727 (that's the minimum allowed value; many RNGs have a much larger range), using `%9` or `%10` will introduce a bias on the order of one part in 3000, i.e., ~.03%. – Pete Becker Mar 15 '19 at 16:46
  • @PeteBecker you're right, but C++ provides the necessary tool to be safe regardless of the ranges, so they must be preferred. – Yann Droneaud Mar 15 '19 at 17:38
  • @YannDroneaud — engineering is about evaluating tradeoffs, not blindly applying “rules”. More complicated and slower is not a clear win over minor bias. – Pete Becker Mar 15 '19 at 20:14