主题:[讨论]rand()怎么就不rand呢?
BlackBlizzad
[专家分:0] 发布于 2006-07-17 01:15:00
[em14]
这个是我程序运行的结果:
Some randomly distributed integers will be printed.
How many do you want to see?3
346 130 10982
Some randomly distributed integers will be printed.
How many do you want to see?20
346 130 10982 1090 11656 7117 17595
6415 22948 31126 9004 14558 3571 22879
18492 1360 5412 26721 22463 25047
Some randomly distributed integers will be printed.
How many do you want to see?60
346 130 10982 1090 11656 7117 17595
6415 22948 31126 9004 14558 3571 22879
18492 1360 5412 26721 22463 25047 27119
31441 7190 13985 31214 27509 30252 26571
14779 19816 21681 19651 17995 23593 3734
13310 3979 21995 15561 16092 18489 11288
28466 8664 5892 13863 22766 5364 17639
21151 20427 100 25795 8812 15108 12666
12347 19042 19774 9169
Some randomly distributed integers will be printed.
How many do you want to see?20
346 130 10982 1090 11656 7117 17595
6415 22948 31126 9004 14558 3571 22879
18492 1360 5412 26721 22463 25047
Some randomly distributed integers will be printed.
How many do you want to see?20
346 130 10982 1090 11656 7117 17595
6415 22948 31126 9004 14558 3571 22879
18492 1360 5412 26721 22463 25047
怎么每次都一样?
回复列表 (共14个回复)
沙发
skybtone [专家分:160] 发布于 2006-07-17 08:34:00
rand()是哪个头文件里的?/?
板凳
boxertony [专家分:23030] 发布于 2006-07-17 10:23:00
rand()生成的“随机数”本来就不是真正的随机数,准确的说应该叫“伪随机数”。这个伪随机数的序列是由一开始的种子(隐含的)决定的,种子不变,伪随机数序列就不会变。要想在程序每次运行时序列都不同,就要使用srand函数来初始化(即每次都使用不同的种子来产生),一般来说可以使用当前的系统时间作为种子。方法如下:
srand((unsigned)(time(NULL));
3 楼
BlackBlizzard [专家分:50] 发布于 2006-07-17 12:04:00
种子?怎么把时间加上去呢??
4 楼
skybtone [专家分:160] 发布于 2006-07-17 12:19:00
大哥能告诉我
rand();和srand();在包含了哪个头文件时才能使用这两个算法吗?谢谢
5 楼
BlackBlizzard [专家分:50] 发布于 2006-07-17 12:23:00
谢谢 2楼的哦~
我改了一下,可以了
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
int main()
{
int i,n;
srand((unsigned)(time(NULL)));
printf("\n%s\n%s","Some randomly distributed integers will be printed.",
"How many do you want to see?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i%7==0)
printf("\n");
printf("%9d",rand());
}
printf("\n");
return 0;
}
结果:
Some randomly distributed integers will be printed.
How many do you want to see?20
29401 26719 5506 8230 12587 23123 32708
4168 23790 2475 6666 24825 18889 8738
26218 7631 9250 24358 32102 11036
Some randomly distributed integers will be printed.
How many do you want to see?20
2520 23053 26148 3910 28365 11495 14225
10712 9946 10422 23812 20944 28711 9300
17168 11306 12593 26156 25254 22195
Some randomly distributed integers will be printed.
How many do you want to see?20
10832 17877 24449 28652 19801 862 3552
4530 13533 10076 17177 23174 27157 13949
10175 26133 11531 13273 21369 18674
但是你讲的什么种子,我不知道 也
6 楼
boxertony [专家分:23030] 发布于 2006-07-17 12:52:00
[quote]种子?怎么把时间加上去呢??[/quote]
我在上面不是都说了嘛。
7 楼
boxertony [专家分:23030] 发布于 2006-07-17 12:52:00
[quote]大哥能告诉我
rand();和srand();在包含了哪个头文件时才能使用这两个算法吗?谢谢[/quote]
stdlib.h
8 楼
boxertony [专家分:23030] 发布于 2006-07-17 12:58:00
[quote]但是你讲的什么种子,我不知道 也[/quote]
这你得去看看随机数生成方面的东东了。
9 楼
rickone [专家分:15390] 发布于 2006-07-17 14:29:00
blackBlizzard? ^_^
记事本->D:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\Rand.c
/***
*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 */
}
10 楼
skybtone [专家分:160] 发布于 2006-07-17 18:06:00
补充:5楼的程序里,我觉得要添加一个头文件:time.h
time(NULL)这个算法是time.h里头的
我来回复