读书人

程序疏失了`求指教

发布时间: 2012-09-29 10:30:01 作者: rapoo

程序出错了```求指教
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main(void)
{

int s,times=0;
srand((unsigned) time(NULL));
while(times<10)
{
s=rand();
printf("%d",s);

}
return 0;
}


--------------------Configuration: Cpp1 - Win32 Debug--------------------
Linking...
Cpp1.obj : error LNK2001: unresolved external symbol "int __cdecl rand(void)" (?rand@@YAHXZ)
Cpp1.obj : error LNK2001: unresolved external symbol "void __cdecl srand(unsigned int)" (?srand@@YAXI@Z)
Debug/Cpp1.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.

Cpp1.exe - 1 error(s), 0 warning(s)

[解决办法]
没什么错误吧,就是你while循环没有结束控制,死循环啊亲,times一直没加
[解决办法]
srand() 包含在 <stdlib.h>中,之于你出现的问题,不知道你是怎么出现的


微软给的例子:
http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k%28RAND%29;k%28DevLang-%22C%2B%2B%22%29&rd=true

// 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 );
}

读书人网 >C语言

热点推荐