我在这里有这个源代码,它有一个必需的随机数生成函数,称为randG(); 问题是当我分叉 5 个孩子并在每个孩子中调用该函数 5 次时,我得到了 5 次相同的数字。有没有办法解决这个问题?我需要每个数字都是单独的和具体的。谢谢!
source code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <stdlib.h>
#include <math.h>
int randG(int mean, int stddev) {
double mu = 0.5 + (double) mean;
double sigma = fabs((double) stddev);
double f1 = sqrt(-2.0 * log((double) rand() / (double) RAND_MAX));
double f2 = 2.0 * 3.14159265359 * (double) rand() / (double) RAND_MAX;
if (rand() & (1 << …Run Code Online (Sandbox Code Playgroud) 对于这个问题的基本程度,我深表歉意,但无论如何,这是我的第一个 Scala 项目,我试图做的就是将数组传递给函数并返回该函数。这是我所拥有的:
def main(args: Array[String]): Unit = {
// 9999 == infinite there should be no reason
// that for the scope of this assignment there
// there should be a value any bigger than this.
var Nodes = 4;
var infi = 9999;
var pathTaken = Array(Nodes);
var pathLens = Array(Nodes);
var paths = Array(Nodes, Nodes);
pathLens = lenInit(pathLens);
}
def pathLens(x : Array[4]): Unit = {
x = (0, 0, 0, 0);
return x;
}
Run Code Online (Sandbox Code Playgroud)
我知道这是非常基本和简单的,但我一生都是一个 C/python …