我想用rand()%2得到随机分配的0,1.可是好像没有实现~
谁有更好的随机函数呢?

解决方案 »

  1.   

    在rand之前加一句srand(TIME(NULL))
      

  2.   

    我这样来实现的,因为我要的是0——1之间的随机数,你可以用我的方法实现,产生的随机0和1!
    double number = rand()/32767.0;//产生随机数
    int num = (int)(((number * 10) + 5) / 10);
      

  3.   

    int i;
    time_t t;
    srand((unsigned) time(&t));
    i = rand();
      

  4.   

    其实使用srand(rand());也不错哦
      

  5.   

    int i;
    time_t t;
    srand((unsigned) time(&t));
    i = rand();
    什么代码?有必要吗?
      

  6.   

    一下代码摘自msdn
    Example// crt_rand.c
    /* This program seeds the random-number generator
     * with the time, then displays 10 random integers.
     */#include <stdlib.h>
    #include <stdio.h>
    #include <time.h>int main( void )
    {
       int i;   /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
        */
       srand( (unsigned)time( NULL ) );   /* Display 10 numbers. */
       for( i = 0;   i < 10;i++ )
          printf( "  %6d\n", rand() );
    }Sample Output   19430
       28222
        9710
       12070
        7513
        9501
        1767
       26041
       11872
        4097