70,023
社区成员




#include <stdlib.h>
#include <stdio.h>
#include <time.h> /*用到了time函数,所以要有这个头文件*/
#define MAX 10
int main( void)
{
int number[MAX] = {0};
int i;
for(i = 0; i < MAX; i++)
{
srand((unsigned) time(NULL)); /*播种子*/
number[i] = rand() % 100; /*产生100以内的随机整数*/
printf("%d ", number[i]);
}
printf("\n");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/************************************************/
/*随机获取[M , N]区间内的浮点数 */
double RandomFloate(int M, int N)
{
int RandomValue1;
double RandomValue2;
time_t t;
srand((unsigned)time(&t));
RandomValue1 = M + rand() % (M - N + 1);
RandomValue2 = rand() / (RAND_MAX + 1);
return RandomValue1 + RandomValue2;
}
/************************************************/
int main()
{
int i;
for(i= 0;i<100; i++)
{
printf("%f\t",RandomFloate(2,5));
}
}
/***
*rand.c - random number generator
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/
#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>
#ifndef _MT
static long holdrand = 1L;
#endif /* _MT */
/***
*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
)
{
#ifdef _MT
_getptd()->_holdrand = (unsigned long)seed;
#else /* _MT */
holdrand = (long)seed;
#endif /* _MT */
}
/***
*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
)
{
#ifdef _MT
_ptiddata ptd = _getptd();
return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );
#else /* _MT */
return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
#endif /* _MT */
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*随机获取[M , N]区间内的浮点数 */
int j =0;
double RandomFloate(int M, int N)
{
/***************************************************/
if(j ==0) //确保仅设置seed一次
{
time_t t;
srand((unsigned)time(&t));
}
j = 1;
/****************************************************/
int RandomValue1;
double RandomValue2;
RandomValue1 = M + (int)(rand() % (M - N + 1));
RandomValue2 = rand() / (double)(RAND_MAX + 1);
return RandomValue1 + RandomValue2;
}
/***************************************************/
int main()
{
int i;
for(i= 0;i<1000; i++)
{
printf("%f\t",RandomFloate(29,518));
}
return 0;
}