有人可以给我一个很好的解释,说明如何实现这一点吗?
例如,假设我有一个初始化为值 {3, 4, 5} 的数组 a[j]。我想构建第二个数组,其中填充的随机数低于零,高于第一个数组中的值。因此 b[i] 将被初始化为以下范围内的随机数:{0-3, 0-4, 0-5}
在 C++ 中实现此目的的好方法是什么?
最佳答案
如果您的系统有arc4random(3)
和 friend (可能确实如此),这真的很容易:
int a[3] = {3, 4, 5};
int b[3];
for (int i = 0; i < 3; i++)
{
b[i] = arc4random_uniform(a[i] + 1);
}
来自上面链接的手册页:
arc4random_uniform()
will return a single 32-bit value, uniformly distributed but less than upper_bound.
关于c++ - 由第二个数组中的值限定的随机数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503910/