std::mt19937 为相同的第一个浮点数 ex(1.2, 1.5) 给出相同的随机浮点数

std::mt19937 gives the same random float for identical first float numbers ex(1.2, 1.5)

我有这个随机浮点函数,看起来像这样:

float randomFloat(float input)
{
    std::mt19937 mt;
    mt.seed(input);
    std::uniform_real_distribution<float> dt(-1,1);

    return dt(mt);
}

如您所见,该函数接受一个输入,return一个介于 -1 和 1 之间的输出

但我的问题是,当我给出输入浮点数时,如果点左侧的数字相同。示例:(1.2, 1.52, 1.658, 1.01...) 随机浮点数将给出相同的值,(为糟糕的英语道歉)

因此输入 1.5 和另一个输入 1.2 将给出相同的 return 值,而输入 1.5 和另一个输入 2.5 将给出不同的值。我该如何解决这个问题?

请记住,我并不是要准确地获取 randomFloat,我的问题有点复杂,但如果我能就这个特定问题获得帮助,其他一切都会很容易解决,而我的原因我说这是我不想让答案告诉我使用 random_device 或者 mt 应该播种一次...我已经知道,this 是我正在做的,如果你真的想知道

谢谢!

期望的种子值是无符号整数类型;传递 float 实际上只是传递所述 float.

的截断整数值

你可以从 signature of seed, which returns result_type, and from the documentation of result_type:

result_type - The integral type generated by the engine. Results are undefined if this is not an unsigned integral type.

std::mt19937 的结果类型为 std::uint_fast32_t,这也是要播种的参数类型,因此,浮点数会被截断。