vs2010产生随机数时的一个问题
头文件bx_suji.h如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned suijishu(int n)
{
unsigned int i;
srand(time(0));
i = rand()%n;
return i;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bx_suiji.h"
void main()
{
char s[4] = {0};
int i=0,n=0;
for(n =0;n<2;n++)
{
for(i=0;i<4;i++)
{
s[i] = 'a'+suijishu(26);
}
for(i = 0;i<4;i++)
{
printf("%c",s[i]);
}
printf("\n");
}
printf("done!\n");
exit(0);
}
#include <stdlib.h>
unsigned suijishu(int n)
{
unsigned int i;
// srand(time(0));// 不需要每次都初始化
i = rand()%n;
return i;
}
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bx_suiji.h"
void main()
{
srand(time(0));// 在这里初始化,就可以了。
char s[4] = {0};
int i=0,n=0;
for(n =0;n<2;n++)
{
for(i=0;i<4;i++)
{
s[i] = 'a'+suijishu(26);
}
for(i = 0;i<4;i++)
{
printf("%c",s[i]);
}
printf("\n");
}
printf("done!\n");
exit(0);
}
/***
*rand.c - random number generator
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/
#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>
/***
*void srand(seed) - seed the random number generator
*
*Purpose:
* Seeds the random number generator with the int given. Adapted from the
* BASIC random number generator.
*
*Entry:
* unsigned seed - seed to seed rand # generator with
*
*Exit:
* None.
*
*Exceptions:
*
*******************************************************************************/
void __cdecl srand (
unsigned int seed
)
{
_getptd()->_holdrand = (unsigned long)seed;
}
/***
*int rand() - returns a random number
*
*Purpose:
* returns a pseudo-random number 0 through 32767.
*
*Entry:
* None.
*
*Exit:
* Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/
int __cdecl rand (
void
)
{
_ptiddata ptd = _getptd();
return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );
}