荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Deny (相信我,没错的!), 信区: Program
标  题: Re: 在VC++中,怎样产生随机数?
发信站: 荔园晨风BBS站 (Mon Nov 19 22:58:11 2001), 转信

rand
Generates a pseudorandom number.

int rand( void );

Routine Required Header Compatibility
rand <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

rand returns a pseudorandom number, as described above. There is no
error return.

Remarks

The rand function returns a pseudorandom integer in the range 0 to
RAND_MAX. Use the srand function to seed the pseudorandom-number
generator before calling rand.

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


【 在 cycker (cycker) 的大作中提到: 】
在C中的
void randomize(void)
int random(int num)
好像不行。

--


--
   │      ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐         ∮
   ││    │▌▌│▌▌▌│▌▌│▌▌▌│▌▌    每一个音符,每一个节拍
   │││  ││││││││││││││∮││    都浸透着我们的汗水、艰辛
   │││  └┴┴┴┴┴┴┴┴┴┴┴┴∮┴┴┘    同时,也都结合着我们的共点
   ┼┼┼────────∮────────────────────
   ┼┼┼──────────────────────────────────

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.1.201]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店