- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用 C++ 编写一个函数,每次返回一个介于 0 和 1 之间的随机 float / double ,每次都具有不同的值。
我尝试了 srand、rand 和 RAND_MAX 的几种不同方向,但每次运行代码时,每次都应更改的某些值具有常量值。我有一个术语 x = 20 * randomnumber() 但每次它都会为 x 返回相同的值。无论我多久运行一次代码。这是我的功能。
double randomnumber()
{
srand(time(NULL))
double r1 = ((double)rand()) / RAND_MAX);
return r1;
}
我想要它做的是生成一个介于 0 和 1 之间的 float ,这样当我乘以另一个整数时,我得到一个介于 0 和该整数之间的值。注意:我知道有一个函数,我可以在 0 和数字之间明确地执行此操作,但我正在编写的代码最好每次都乘以一个随机数。
提前致谢。
最佳答案
最好的方法是使用 C++ 的随机数库,不是 rand()
。
我们可以很容易地做到这一点:
#include <random>
double randomnumber() {
// Making rng static ensures that it stays the same
// Between different invocations of the function
static std::default_random_engine rng;
std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(rng);
}
每次都会生成一个新的随机数,并且每次运行程序时都会生成相同的随机数序列。如果我跑
int main() {
for(int i = 0; i < 10; i++) {
std::cout << randomnumber() << '\n';
}
}
那我看看
0.131538
0.45865
0.218959
0.678865
0.934693
0.519416
0.0345721
0.5297
0.00769819
0.0668422
随机初始化生成器。如果您想在每次运行程序时生成不同的随机数,则必须使用随机种子初始化生成器。生成一个非常容易:
auto getRandomSeed()
-> std::seed_seq
{
// This gets a source of actual, honest-to-god randomness
std::random_device source;
// Here, we fill an array of random data from the source
unsigned int random_data[10];
for(auto& elem : random_data) {
elem = source();
}
// this creates the random seed sequence out of the random data
return std::seed_seq(random_data + 0, random_data + 10);
}
一旦我们有了这个种子,我们就可以修改 randomnumber()
来创建带有随机种子的生成器:
#include <random>
double randomnumber() {
// Making rng static ensures that it stays the same
// Between different invocations of the function
static auto seed = getRandomSeed();
static std::default_random_engine rng(seed);
std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(rng);
}
关于c++ - RNG 函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56435506/
我开发了一个 RNG 算法并生成随机数,现在我想测试生成的输出的随机性。我从 nist(sts) 下载了一个测试套件。有一个选项可以提供输入二进制文件来测试随机性。但我不知道如何生成这些二进制文件。我
因此,我编写了一个函数(以及一个 RNG 函数,上述函数调用该函数)来将随机数量的星号打印到控制台窗口,直到达到 90 个空格。星号代表汽车的运动,90个空格是轨道的长度。下面我包含的代码打印随机数量
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我正在尝试为我的模拟器重现一些行为。 我有一个百分比支票,从 30% 到 70% 不等。问题是它不是严格随机的。这是我提取的实时数据: https://docs.google.com/spreadsh
这个问题在这里已经有了答案: How to generate a random integer in the range [0,n] from a stream of random bits wit
我目前正在实现一个代表 52 张牌的牌组的 Deck 类。它使用 boost Random 库来洗牌代表卡片的整数。 #include #include #include "constants.h
在本文的最后一个示例 (http://www.daniweb.com/software-development/cpp/threads/1769/c-random-numbers) 中,作者声称这是一
我想使用 在我的 C++ 应用程序中使用多个 RNG .播种它们的最佳方法是什么?我担心当使用来自单个 RNG 的随机数为每个 RNG 播种时,来自不同 RNG 的随机数会过于相关。 最佳答案 如果
现在我正在使用 Mersenne Twister RNG 并执行 Fisher-Yates 洗牌算法 100 次: std::vector shufCards; for(int i =
我正在开发一款纸牌游戏,我需要洗牌算法来做得很好,并且每次游戏运行时都不同,并且没有可预测的纸牌序列。 我正在使用 Mersenne twister 算法,但它仍然需要一个种子,所以实际上,虽然它产生
我可以使用两种方法中的一种来创建具有两个重要特征的伪随机数序列 - (1) 它可以在不同的机器上重现,并且 (2) 该序列从不重复范围内的数字,直到所有数字都被发出. 我的问题是 - 这些方法中的任何
我有一段冗长而复杂的源代码,它使用带有修复种子的随机数生成器。 这段代码是一个模拟器,这个模拟器的参数就是这个RNG给出的随机值。当我在同一台机器上执行代码时,无论我尝试多少次,输出都是一样的。但是当
我在想这个问题。我听说全局变量不好,它们会损害代码的可维护性、可用性、可重用性等。但在这种情况下,我还能做什么呢?也就是说,我有一个“伪随机数生成器”(PRNG),正如人们所知,它们涉及一种内部状态,
回复 this question我运行了以下 VBA 实验: Sub Test() Dim i As Long, A As Variant Dim count1 As Long, co
我知道我可以用例如设置 RNGversion RNGversion("3.5.2") 但是是否可以查询我当前使用的 R 版本? 编辑: 我的问题是版本 3.6.0 发生了一些变化,这在 RNGkind
我今天刚开始学习JavaScript,这个很简单的问题给我带来了麻烦。这只是较大代码段的一部分,但我将其隔离以尝试修复它。这是: document.write(blarg()); func
我制作了一个随机数生成器,如果有人在没有数字的情况下使用它,它会崩溃如何确保用户输入是否是字符串,它不会崩溃,而是说“无效参数”? static int random; static String s
好吧,我正在编写一个与 cpu 对抗的程序,但每次它都会先失败然后致命,我会分解我的代码,以便让你们更容易提供帮助。 进口: import javax.swing.JOptionPane; impor
我的问题是http://rcpp-devel.r-forge.r-project.narkive.com/qJMEsvOK/setting-the-r-random-seed-from-rcpp的后续
作为一个学习 Haskell 的 Java 人,我已经习惯了思考一切的新方法,但我花了半天时间尝试用简单的 RNG 实现一些东西,但一无所获。在 Java 中,我可以创建一个静态 RNG 并使用 Cl
我是一名优秀的程序员,十分优秀!