简体   繁体   English

带范围的随机数生成器? C ++

[英]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. 我正在尝试为我的Ebay业务制作一个长度为15位数字的C ++程序,但我希望前5位数字相同。

Like ex: 152328476529876 PIN: 1000 152323123642345 PIN: 9433 152321254213432 PIN: 3222 如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. 我尝试制作一个随机数生成器,但无法到达前5位相同的地方,但最后10位相同的地方。 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... 我遇到的问题是前5个数字是相同的,这就是我想要的方式,但是14100仍然是相同的,因此也应该随机分配,而不仅仅是后5个数字...

Plus the pins only stay at 1000, they never go above 1999 etc. 再加上引脚仅停留在1000,它们从未超过1999等。

Instead of using rand() and modulo operations, you should make use of the <random> header from C++11. 不应使用rand()和模运算,而应使用C ++ 11中的<random>标头。 Using Walter E. Brown's function toolkit from n3847 , you could use: 使用n3847的 Walter E. Brown的功能工具包,您可以使用:

#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

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. 您的问题是,使用rand()生成一个随机数会为您提供一个介于0到至少32767之间的数字(具体取决于您的平台,请参见此处 ),而这个数字还不够大。 Another problem is that the datatype int does not have enough bits to store such a large number. 另一个问题是数据类型int没有足够的位来存储这么大的数字。

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) int使用至少16位可以存储从0到65536(如果是无符号)的数字
  • long uses at least 32 bits which can store numbers from 0 to 4294967296 (in unsigned) long使用至少32位,可以存储0到4294967296之间的数字(无符号)
  • long long uses at least 64 bits which can store numbers from 0 to 18446744073709551616 (if unsigned) long long使用至少64位,可以存储0到18446744073709551616(如果是无符号)之间的数字

So you already have a problem with your max values since they do not fit inside an int. 因此,您的最大值已经存在问题,因为它们不适合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. 如果要确定数字的大小,可以使用stdint.h并使用uint64int32等指定ints

What you want is 10 random digits. 您想要的是10个随机数字。 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 我认为Pin的问题类似

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM