65,211
社区成员
发帖
与我相关
我的任务
分享
// crt_rand.c
// This program seeds the random-number generator
// with the time, then exercises the rand function.
//
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void SimpleRandDemo( int n )
{
// Print n random numbers.
int i;
for( i = 0; i < n; i++ )
printf( " %6d\n", rand() );
}
void RangedRandDemo( int range_min, int range_max, int n )
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
int i;
for ( i = 0; i < n; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min;
printf( " %6d\n", u);
}
}
int main( void )
{
// Seed the random-number generator with the current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );
SimpleRandDemo( 10 );
printf("\n");
RangedRandDemo( -100, 100, 10 );
}
22036
18330
11651
27464
18093
3284
11785
14686
11447
11285
74
48
27
65
96
64
-5
-42
-55
66
研究下这两头文件
#include<cstdlib>
#include<ctime>
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include "RandomNumber.h"
using namespace RN;
RandomNumber::RandomNumber()
{
srand((unsigned)time(NULL));//调用标准C的函数产生随机数,
}
int RandomNumber::getRandomNumber()
{
int res=rand()%100;
if(res%2==0)
{
res++;
}
res+=100;
return res;
}
RandomNumber::~RandomNumber()
{
}