您的位置:首页 > 其它

generate the random number between a to b

2012-09-09 19:35 591 查看
/**********************************************************
@brief: generate the random number between a to b
**********************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void RangedRandDemo( int range_min, int range_max, int n )
{
int i, u;
// Seed the random-number generator with the current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );
for ( i = 0; i < n; i++ )
{
//u = rand() % (range_max - range_min + 1) + range_min; //[a,b]

//u = rand() % (range_max - range_min ) + range_min;  //[a,b)

//u = rand() % (range_max - range_min ) + range_min + 1;  //(a,b]

u = rand() % (range_max - range_min - 1) + range_min + 1;  //(a,b]

printf( "  %6d\n", u);
}
}

int main( void )
{
printf("\n");
RangedRandDemo( -2, 2, 10 );
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐