能不能控制随机数生成的范围srand()
rand()好像不行呀

解决方案 »

  1.   

    srand( (unsigned)time( NULL ) );
    int i = rand();
      

  2.   

    可以啊
    rand()%1000;
    1000以内的数
      

  3.   

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>void 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() );
    }
    Output    6929
        8026
       21987
       30734
       20587
        6699
       22034
       25051
        7988
       10104