Multithreading 这种生成随机数的方法安全有效吗?
前几天我在做一些娱乐性的低级编程,在我的代码中出现了这样的东西(使用伪代码): 投币计划Multithreading 这种生成随机数的方法安全有效吗?,multithreading,random,Multithreading,Random,前几天我在做一些娱乐性的低级编程,在我的代码中出现了这样的东西(使用伪代码): 投币计划 char out = '0'; void thread(){ out = '1';} int main(){ // Creates a thread that will run the function we provided // The main() will not wait for this thread to finish runInOwnThread(thread);
char out = '0';
void thread(){ out = '1';}
int main(){
// Creates a thread that will run the function we provided
// The main() will not wait for this thread to finish
runInOwnThread(thread);
printr("%d", out);
return 0;
}
现在,在我们的shell中,我们运行cointost
programx
多次,我们将得到如下结果:
0011011100001000
我们可以把它转换成十进制,得到一个随机数
我的问题不是这是否是一种创建随机数的有效方法,而是我们可以知道最终结果是否足够随机以供使用。不,使用竞争条件不是生成随机数的好方法。结果将是不确定的,但结果可能对一个结果有很大影响 如果您不关心这些值的权重过大(这对您来说已经足够随机),那么就继续吧