Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 这种生成随机数的方法安全有效吗?_Multithreading_Random - Fatal编程技术网

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
program
x
多次,我们将得到如下结果:

0011011100001000

我们可以把它转换成十进制,得到一个随机数


我的问题不是这是否是一种创建随机数的有效方法,而是我们可以知道最终结果是否足够随机以供使用。不,使用竞争条件不是生成随机数的好方法。结果将是不确定的,但结果可能对一个结果有很大影响

如果您不关心这些值的权重过大(这对您来说已经足够随机),那么就继续吧