【发布时间】:2013-10-06 02:10:48
【问题描述】:
我想在不使用 rand/srand urandom 等的情况下模拟随机交付 52 张标准卡...
这是我的随机数函数
int rand2(int lim)
{
static int a = 34; // could be made the seed value
a = (a * 32719 + 3) % 32749;
return ((a % lim) + 1);
}
我将知道卡片是否已经弹出的结构(0 = 否,1 是)
typedef struct s_game
{
int *cards;
int state;
unsigned int dat_rand;
} t_game;
int main()
{
t_game game;
int i;
int rd;
i = 0;
game.cards = malloc(sizeof(*game.cards) * 52);
while(i < 52)
{
rd = rand2(52);
if(game.cards[rd] == 0)
{
game.cards[rd] = 1;
printf("i:%d\n rd: %d\n", i, rd);
i++;
}
}
}
但我的输出总是一样的,每张卡片都是同时交付的,所以我正在寻找更好的随机函数或不同的方式来填充我的交付
【问题讨论】:
-
是什么让你觉得你的函数比标准函数更随机?
-
谷歌搜索 "random number c" 没有结果吗?我不相信……
-
@Saxtheowl 为什么?有很多关于随机性的高级数学。自己做肯定会产生非随机数。 (除非你有数学博士学位,但你不会在这里问这个问题)。
-
可能重复:stackoverflow.com/questions/6275593/… 不要发明自己的加密/prng!
-
@Shahbaz “更重要的位 [是] 更随机” 有信息链接来支持该声明吗?不要怀疑,只是感兴趣。没关系我发现了什么:eternallyconfuzzled.com/arts/jsw_art_rand.aspx