Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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
C rand()未返回随机值_C_Linux_Gcc_Matrix_Libc - Fatal编程技术网

C rand()未返回随机值

C rand()未返回随机值,c,linux,gcc,matrix,libc,C,Linux,Gcc,Matrix,Libc,这是一个生成随机厄米矩阵的小代码 在每次调用rand()之前,我都调用了srand()。但输出仍然没有随机性 我使用c99的复杂数据类型特性创建了一个埃尔米特矩阵。我不确定我错在哪里:( 编辑在main()中调用srand实际上解决了这个问题。谢谢大家 [aft@centos-c physics-numaric]$ ./a.out 31.000000 + 0.000000 *i 81.000000 + 75.000000 *i 81.000000 + 75.000000 *i

这是一个生成随机厄米矩阵的小代码

在每次调用rand()之前,我都调用了srand()。但输出仍然没有随机性

我使用c99的复杂数据类型特性创建了一个埃尔米特矩阵。我不确定我错在哪里:(

编辑在main()中调用srand实际上解决了这个问题。谢谢大家

[aft@centos-c physics-numaric]$ ./a.out 
31.000000 + 0.000000 *i    81.000000 + 75.000000 *i    81.000000 + 75.000000 *i     81.000000 + 75.000000 *i    81.000000 + 75.000000 *i    

81.000000 + -75.000000 *i    53.000000 + 0.000000 *i    69.000000 + 57.000000 *i    69.000000 + 57.000000 *i    69.000000 + 57.000000 *i    

69.000000 + 57.000000 *i    69.000000 + -57.000000 *i    27.000000 + 0.000000 *i    93.000000 + 11.000000 *i    93.000000 + 11.000000 *i    

93.000000 + 11.000000 *i    69.000000 + -57.000000 *i    93.000000 + -11.000000 *i    58.000000 + 0.000000 *i    76.000000 + 78.000000 *i    

76.000000 + 78.000000 *i    69.000000 + -57.000000 *i    93.000000 + -11.000000 *i    76.000000 + -78.000000 *i    67.000000 + 0.000000 *i    

在每次调用
rand
之前,不要调用
srand
。在程序启动时调用它一次


不要调用
srand
内部循环。只调用一次

srand(time(NULL));
for( ;i < MATSIZE; i++)
{
    // ... calls to rand()   
}
srand(时间(空));
对于(;i
否则,使用相同的种子为随机生成器设定种子(因为它足够快,可以获得相同的时间)


顺便说一句,很多时候我发现为程序/序列创建一个init函数是非常传统的,在这里我初始化了很多东西,包括随机生成(例如调用
srand()

你不想用
srand()作为随机数生成器的种子
每次!只需在程序开始时调用它一次。然后调用
rand()
获取下一个随机数。

不要这样做:

rand()%RAND_RANGE + 0*I;
,因为如果RAND_RANGE和RAND_MAX+1不相除,则会导致较低的值过采样。(几乎总是这样)

另外:在大多数情况下,基于时间(NULL)重新启动生成器将以完全相同的值重新启动,因为时间的粒度为1秒

最终:rand_max将至少有15位(32K)的随机数据。较旧的系统实际上可能只提供15位,周期为32K

更新:这是的一个代码段。urnd()函数尝试返回一个介于0和范围之间的无偏值。该测试可能可以更优雅地执行

typedef unsigned long long BigThing;

unsigned int urnd(unsigned int range)
{
    static bool flag = FALSE;

    if (flag == FALSE) {
#if defined(__mac_os) || defined(DOS)
        srand(time(NULL));
#else
        srand48(time(NULL));
#endif
    }
    flag = TRUE;
#if defined(__mac_os) || defined(DOS)
    return rand()%range;
#else

if (range <= 1) return 0;

while(1)        {
    BigThing val, box;
#if WANT_RDTSC_RANDOM
    val = rdtsc_rand();
#else
    val =  lrand48();
#endif
/* we need this to avoid oversampling of the lower values.
 * Oversampling the lower values becomes more of a problem if (UNSIGNED_MAX/range) gets smaller
 */
    box = val / range;
    if ((1+box) *range < range) continue;
    return val % range;
        }
#endif
}
typedef无符号long-long-BigThing;
无符号整数urnd(无符号整数范围)
{
静态布尔标志=假;
如果(标志==FALSE){
#如果已定义(uu mac_os)| |已定义(DOS)
srand(时间(空));
#否则
srand48(时间(空));
#恩迪夫
}
flag=TRUE;
#如果已定义(uu mac_os)| |已定义(DOS)
返回rand()%范围;
#否则

if(range@aftnix:我认为有一个计时器,所以你只能在提问10分钟后接受答案。Charlesworth的人在这些stack exchange应用程序设计背后真的考虑了很多。cool dynamics:)我点击了“标志”这是一个偶然的答案。如何撤销它?那么你的建议是什么?我应该如何获取一个随机值,并正确地保持一个范围?想法是:如果你从范围的顶部获得一个样本,就在环绕发生之前:拒绝它并重新绘制。例如,如果RAND_MAX是10,而你的RAND_范围是4:拒绝值>=8,否则返回值%RAND_RANGE。(否则,{0,1}的值在输出中发生UC错误的几率大于{2,3})我将在回复中添加一个片段。
rand()%RAND_RANGE + 0*I;
typedef unsigned long long BigThing;

unsigned int urnd(unsigned int range)
{
    static bool flag = FALSE;

    if (flag == FALSE) {
#if defined(__mac_os) || defined(DOS)
        srand(time(NULL));
#else
        srand48(time(NULL));
#endif
    }
    flag = TRUE;
#if defined(__mac_os) || defined(DOS)
    return rand()%range;
#else

if (range <= 1) return 0;

while(1)        {
    BigThing val, box;
#if WANT_RDTSC_RANDOM
    val = rdtsc_rand();
#else
    val =  lrand48();
#endif
/* we need this to avoid oversampling of the lower values.
 * Oversampling the lower values becomes more of a problem if (UNSIGNED_MAX/range) gets smaller
 */
    box = val / range;
    if ((1+box) *range < range) continue;
    return val % range;
        }
#endif
}