Tag: 随机

在Objective-C / C中生成随机高斯双精度

我正在尝试在Objective-C中生成随机高斯双random.nextGaussian (与Java中的random.nextGaussian相同)。 但是rand_gauss()似乎不起作用。 有人知道实现这一目标的方法吗?

为什么在设置-std = c99时gcc不能找到random()接口?

我在源代码的顶部做”#include ” 。 示例编译: /usr/bin/colorgcc -std=c99 -fgnu89-inline -g -Wall -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../ -O3 -o f8 f8.c In file included from f8.c:7: ctype-cmp.c: In function ‘randomized’: ctype-cmp.c:48: warning: implicit declaration of function ‘random’ ctype-cmp.c: In function ‘main’: ctype-cmp.c:153: warning: implicit declaration of function ‘srandom’ ais@xcalibur:t$ 当我关闭-std = c99时,无法找到函数isfinite()。 所以我确实想要使用-std = c99这个和其他原因。 有什么技巧我不见了?

libc随机数生成器有缺陷吗?

考虑一种算法来测试在特定次数的尝试之后从一组N个唯一数字中挑选某个数字的概率(例如,N = 2,轮盘中的概率是什么(没有0),它需要X尝试黑赢?) 对此的正确分布是pow(1-1 / N,X-1)*(1 / N)。 但是,当我使用下面的代码测试时,X = 31处始终存在深沟,独立于N,并且独立于种子。 这是一个内在的缺陷,由于PRNG的实施细节在使用中无法防止,这是一个真正的错误,还是我忽略了一些明显的东西? // C #include #include #include int array[101]; void main(){ int nsamples=10000000; double breakVal,diffVal; int i,cnt; // seed, but doesn’t change anything struct tms time; srandom(times(&time)); // sample for(i=0;i<nsamples;i++){ cnt=1; do{ if((random()%36)==0) // break if 0 is chosen break; cnt++; }while(cnt<100); array[cnt]++; } // […]

使用循环在数组中生成唯一随机数

所以问题是开发一个[5] [5]表,每个表包含1-100的唯一数字( 没有重复 ) 所以这就是我提出的: #include #include #include int main() { int outerLoop; int innerLoop; int board[5][5]; /* Array Name And Size*/ /* seeds the random number generator*/ srand(time(NULL)); int number; number = rand() % 101; /* Start a loop to generate a random integer between 1 and 100 and assign it into the array. […]

随机数function失灵

我有一个非常简单的iPhone应用程序,需要1-100的随机整数。 我有一个按钮,调用随机数function,然后显示它。 -(IBAction)buttonReleased; { srandom(time(NULL)); int theNum = random() % 100 + 1; numberDisplay.text = [NSString stringWithFormat:@”%d”, theNum]; } 问题是,如果我快速按下按钮,有时它将不会显示新的随机数。

为什么在兰特使用1103515245?

我正在谈论C标准中这种令人惊讶的简单rand()实现: static unsigned long int next = 1; int rand(void) /* RAND_MAX assumed to be 32767. */ { next = next * 1103515245 + 12345; return (unsigned)(next/65536) % 32768; } 从这篇维基百科文章我们知道乘数a (在上面的代码a = 1103515245 )应该只满足2个条件: a – 1可被m所有素因子整除。 (在我们的例子中, m = 2^32 ,int的大小,所以m只有一个素因子= 2) 如果m是4的倍数,则a – 1是4的倍数。 (32768是4的倍数,也是1103515244) 为什么他们选择了这样一个奇怪的,难以记住的,“男人,我厌倦了这些随机数字,写下任何”数字,如1103515245? 也许有一些明智的理由,这个数字在某种程度上比另一个更好? 例如,为什么不设置a = 20000000001 ? 它更大,更酷,更容易记住。

C rand()缺乏随机性

我使用rand()生成0或1( rand() % 2 )。 我正在使用当前时间播种它( srand(time(NULL)) )。 经过多次调试后,我意识到rand()永远不会连续返回偶数(奇数)16次或更多次。 这是一个已知的问题? C有更好的PRNG吗? 我使用Visual Studio 2010在Windows 7上运行。

用C语言运行rand()并使用Shell Scripts循环

我有一个C代码文件(调用“ test.c ”,输出是“ test.out ”),如下所示,只是一个简单的随机数输出: int main() { srand(time(NULL)); double r = (double)rand() / (double)RAND_MAX; printf(“%f\n”, r); return 0; } 现在我使用shell脚本在Linux中运行“ test.out ”5次,代码如下: for i in 1 2 3 4 5 do test.out done 但结果如下所示 0.840188 0.840188 0.840188 0.840188 0.840188 它显示相同的随机数。

C中的随机数

for(i = 0; i < n; i++){ srand(time(NULL)); printf("%d ", time(NULL)); for(j = 0; j < (n-1); j++){ a[i][j] = rand(); } } 我尝试生成随机数,但它们是相同的…我尝试srand(i * time(NULL)) 。 不管……我该怎么办? 数组声明: int** a; int i; printf(“Enter array size: “); scanf(“%d”, &n); a = (int**)calloc(n, sizeof(int)); for(i = 0; i < n; i++) a[i] = (int*)calloc(n-1, sizeof(int));

总是由rand()给出的重复数字

我一直使用rand(),它总是连续几次给我相同的值。 我尝试在循环之前使用srand(time(NULL)),但它没有帮助…