michael
michael

Reputation: 9

Random Number generator with range? C++

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.

Upvotes: 0

Views: 590

Answers (2)

Ralf Stubner
Ralf Stubner

Reputation: 26823

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

Upvotes: 1

Mr.Yellow
Mr.Yellow

Reputation: 732

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

Upvotes: 0

Related Questions