C语言 C 中的 random(int) 和 randomize()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19976634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:57:57  来源:igfitidea点击:

random(int) and randomize() in C

crandom

提问by Jesús Ros

I want to generate pseudo-random integers in a given range without introducing the skew that results from the use of rand()%N.

我想在给定范围内生成伪随机整数,而不会引入因使用rand()%N.

I have read about the functions random()and randomize()that seem to substitute the rand()and srand()functions but returning directly an integer in the range given as the parameter of the random()function. In both cases, the functions seem to be in the stdlib.hlibrary.

我已经阅读了有关这些函数的信息random()randomize()它们似乎替代了rand()srand()函数,但直接返回了作为random()函数参数给出的范围内的整数。在这两种情况下,函数似乎都在stdlib.h库中。

The problem I have is that I cannot make these functions work somehow. Here's a small test code I made to test the functions.

我的问题是我无法使这些功能以某种方式工作。这是我为测试功能而制作的一个小测试代码。

#include <stdio.h>
#include <stdlib.h>

int main(){
 randomize(); 
 printf("%d\n",random(100));
 return 0;
}

At compilation with gcc -o test test.cit gives an error

在用gcc -o test test.c它编译时会出错

test.c: In function ‘main':
test.c:6: error: too many arguments to function ‘random'

test.c: In function ‘main':
test.c:6: error: too many arguments to function ‘random'

As far as I know the function random()only takes one argument which is an integer to determine the range of the numbers given. What am I doing wrong?

据我所知,该函数random()只接受一个参数,即一个整数来确定给定数字的范围。我究竟做错了什么?

EDIT: It seems that those correspond to some TurboC old things. So the question is now, how to make "truly" random integers in the sense that they are not skewed? My approach is (as suggested by Vatine)

编辑:似乎那些对应于一些 TurboC 旧东西。所以现在的问题是,如何在不偏斜的意义上制作“真正的”随机整数?我的方法是(如 Vatine 所建议的)

#include <stdio.h>
#include <stdlib.h>

int main(){
  srand(time(NULL));
  printf("%d\n",rand()/(RAND_MAX/100));
  return 0;
}

which seems to yield a correct result. Is this adding some bias or the results have at least equal probability of falling into any of the numbers in the range?

这似乎产生了正确的结果。这是增加了一些偏差还是结果至少有相同的概率落入该范围内的任何数字?

采纳答案by Maxime Chéramy

man randomgives me that:

man random给我:

   #include <stdlib.h>

   long int random(void);

So the error message is correct: there are too many arguments to function ‘random'.

所以错误信息是正确的:函数“随机”的参数太多。

I don't know the function randomizeand the randomfunction I am aware of doesn't take any argument.

我不知道这个函数randomize,我知道的函数random不接受任何参数。

But I did find this on the Internet (careful, you should not use this code, I'm just saying):

但我确实在互联网上找到了这个(小心,你不应该使用这段代码,我只是说):

#ifndef __TURBOC__
#define randomize() srand48(getpid())
#define random(x) (lrand48() % x)
#endif

So maybe it's a turbo C old thing? I also found this examplethat tends to confirm it's a Turbo C specific function.

所以也许这是涡轮增压C的老东西?我还发现这个例子倾向于确认它是一个 Turbo C 特定的函数。

?How to generate a random number in C

?如何在C中生成一个随机数

If you want to keep using randand srand, you can do the following:

如果您想继续使用randand srand,您可以执行以下操作:

int random(int N) {
    return (double)rand()/RAND_MAX * N;
}

But you can also look for other random generators such as the Mersenne Twister. There are plenty implementations available.

但是您也可以寻找其他随机生成器,例如 Mersenne Twister。有很多可用的实现。

回答by Vatine

This is incorrect. The signature for randomis long int random(void);.

这是不正确的。的签名randomlong int random(void);

You can usually get better pseudo-random ranges by dividing by RAND_MAX/Nrather than using modulus (the PRNGs use tend to be more predictable in the lower bits than in the higher bits), but if you are concerned about this, I would suggest using a battery of statistical tests to see how much bias you actually have.

您通常可以通过除以RAND_MAX/N而不是使用模数来获得更好的伪随机范围(PRNG 在低位中使用往往比在高位中更可预测),但如果您对此感到担忧,我建议您使用电池统计测试,看看你有多少偏见。