怎样用rand函数?

kaishuinanhai 2010-12-29 01:13:32
怎样用rand()产生N个随机整数(20000以上);
...全文
263 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eric_SEU 2010-12-29
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
#include <ctime>
#include <iomanip>

int main()
{
srand(time(0));
int a[100];
for(int i=0 ;i <100 ; i++)
{
a[i]= rand();
}
return 0;
}//这是一个产生随机数的例子 ,楼主做参考吧
一根烂笔头 2010-12-29
  • 打赏
  • 举报
回复
int rand ( void );

Generate random number
Returns a pseudo-random integral number in the range 0 to RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in <cstdlib>. Its default value may vary between implementations but it is granted to be at least 32767.

A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014

Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

Parameters
(none)

Return Value
An integer value between 0 and RAND_MAX.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


/* rand example: guess the number */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
int iSecret, iGuess;

/* initialize random seed: */
srand ( time(NULL) );

/* generate secret number: */
iSecret = rand() % 10 + 1;

do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);

puts ("Congratulations!");
return 0;
}



Output:


Guess the number (1 to 10): 5
The secret number is higher
Guess the number (1 to 10): 8
The secret number is lower
Guess the number (1 to 10): 7
Congratulations!



In this example, the random seed is initialized to a value representing the second in which the program is executed (time is defined in the header <ctime>). This way to initialize the seed is generally a good enough option for most randoming needs.

See also
hawl2004 2010-12-29
  • 打赏
  • 举报
回复
srand((unsigned)time(NULL));
cnlm2 2010-12-29
  • 打赏
  • 举报
回复
int num = rand()%20000 + 20000;

这个就可以产生20000以上了
一根烂笔头 2010-12-29
  • 打赏
  • 举报
回复
int rand ( void );

Generate random number
Returns a pseudo-random integral number in the range 0 to RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in <cstdlib>. Its default value may vary between implementations but it is granted to be at least 32767.

A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014

Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

Parameters
(none)

Return Value
An integer value between 0 and RAND_MAX.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


/* rand example: guess the number */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
int iSecret, iGuess;

/* initialize random seed: */
srand ( time(NULL) );

/* generate secret number: */
iSecret = rand() % 10 + 1;

do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);

puts ("Congratulations!");
return 0;
}



Output:


Guess the number (1 to 10): 5
The secret number is higher
Guess the number (1 to 10): 8
The secret number is lower
Guess the number (1 to 10): 7
Congratulations!



In this example, the random seed is initialized to a value representing the second in which the program is executed (time is defined in the header <ctime>). This way to initialize the seed is generally a good enough option for most randoming needs.

See also
一根烂笔头 2010-12-29
  • 打赏
  • 举报
回复

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
srand((int )time(NULL));
for (int i=0; i<1000; i++)
{
int nNum = 20000+rand() % (RAND_MAX-20000);//20000到52767的随机数
}
}

kaishuinanhai 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 matrixcl 的回复:]
引用 5 楼 kaishuinanhai 的回复:

引用 4 楼 anhuihulei 的回复:
#include <time.h>

srand((int )time(NULL));
int nNum = rand() + 20000;//20000到52767的随机数
rand()范围(0~32767)
怎样体现出N个呢


C/C++ code


srand(……
[/Quote]这不是取一百个数了,这个N也太不合理了,但是谢谢你咯,还是用cin>>N;来确定吧!
matrixcl 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kaishuinanhai 的回复:]

引用 4 楼 anhuihulei 的回复:
#include <time.h>

srand((int )time(NULL));
int nNum = rand() + 20000;//20000到52767的随机数
rand()范围(0~32767)
怎样体现出N个呢
[/Quote]


srand((int )time(NULL));

for (int i=0; i<100; i++)
{
int nNum = rand() + 20000;//20000到52767的随机数
}

kaishuinanhai 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 anhuihulei 的回复:]
#include <time.h>

srand((int )time(NULL));
int nNum = rand() + 20000;//20000到52767的随机数
rand()范围(0~32767)
[/Quote]怎样体现出N个呢
anhuihulei 2010-12-29
  • 打赏
  • 举报
回复
#include <time.h>

srand((int )time(NULL));
int nNum = rand() + 20000;//20000到52767的随机数
rand()范围(0~32767)
就想叫yoko 2010-12-29
  • 打赏
  • 举报
回复
百度百科 rand srand
十八道胡同 2010-12-29
  • 打赏
  • 举报
回复
先设置时间种子 然后rand()
龙哥依旧 2010-12-29
  • 打赏
  • 举报
回复
// 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 );
}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧