DOCX

srand

By Sheila Jones,2014-09-08 11:24
14 views 0
srandsrand

srand

    Sets a random starting point.//设置一个随机数的种子

    void srand( unsigned int seed );//定义一个函数

    Routine//Required Header// Compatibility

    程序 需要的头文件 运行环境

    srand <stdlib.h> ANSI, Win 95, Win NT

    For additional compatibility information, see Compatibility in the Introduction. Libraries

    LIBC.LIB Single thread static library, retail version

    LIBCMT.LIB Multithread static library, retail version

    MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

    None//不返回任何值

    Parameter

    Seed//参量

    Seed for random-number generation//种子为随机数生成

    Remarks

    The srand function sets the starting point for generating a series of pseudorandom

    integers. //srand函数的功能是为生成伪随机数设置一个起点To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves;检索; the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.

    Example

/* 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>

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

Report this document

For any questions or suggestions please email
cust-service@docsford.com