我在循环中有多次运行并尝试在每次运行中创建一个不同但伪随机的整数。不幸的是,创建的第一个数字始终为0.
有趣的是,这只是通过import scrapy
class QuotesItem(scrapy.Item):
quoteAuthor = scrapy.Field()
quoteText = scrapy.Field()
生成随机整数的情况。如果我将其替换为std::uniform_int_distribution<int>
,我将获得所需的行为。
创建种子时是否有任何特殊规则或为什么第一个生成的整数始终为0?
以下是我的问题的最小例子:
std::uniform_real_distribution<double>
结果是例如:
#include <iostream>
#include <random>
int main() {
int number_of_runs = 5;
for (int i = 0; i != number_of_runs; ++i) {
// create random engine
std::default_random_engine my_generator;
unsigned my_seed = i; // should be pseudo-random, but different for every run
my_generator.seed(my_seed);
// create distribution
int upper_limit = (i+1)*10; // only example, but different for every run
std::uniform_int_distribution<int> my_distribution(0, upper_limit);
// create random numbers
std::cout << "run #" << i+1 << "\n";
std::cout << "random 1: " << my_distribution(my_generator) << "\n";
std::cout << "random 2: " << my_distribution(my_generator) << "\n";
std::cout << "random 3: " << my_distribution(my_generator) << "\n";
std::cout << "random 4: " << my_distribution(my_generator) << "\n";
std::cout << "random 5: " << my_distribution(my_generator) << "\n" << std::endl;
}
return 0;
}