我做的是信道编码的仿真程序,结构如下: main() for(i=0; i<FRAME_NUM; i++) 我的问题是:如果我在main()函数中调用srand()来初始化随机数种子,是不是只有在main()函数中调用rand()才能识别这个已设置的随机数种子,而在encode()、channel()这样的子函数中调用rand()就不能识别,必须在这些子函数中也写上srand()来设置随机数种子才可以? 我对srand()、rand()的运行机理不了解,只能按照c语言的规则想问题,觉得给函数内部传一个参数(比如随机数种子) ,要么作为函数参数传递,要么就定义成全局变量才可以啊。 请大家不吝赐教 谢谢啦! |
|
5分 |
里面也用得到,问题你只是在启动的时候srand了啊。如果性能允许,建议每次rand之前都srand一把
|
15分 |
楼上胡扯!
放在main函数开始的地方调且只调一次! C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c /*** *rand.c - random number generator * * Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: * defines rand(), srand() - random number generator * *******************************************************************************/ #include <cruntime.h> #include <mtdll.h> #include <stddef.h> #include <stdlib.h> /*** *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 ) { _getptd()->_holdrand = (unsigned long)seed; } /*** *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 ) { _ptiddata ptd = _getptd(); return( ((ptd->_holdrand = ptd->_holdrand * 214013L + 2531011L) >> 16) & 0x7fff ); } |
看到源码,似乎明白了些什么
|
|
谢谢zhao4zhong1 虽然还是有疑惑 但至少知道怎么用了
|
|
知道看源码的重要性了吧。 |
|
提醒楼主:rand()函数返回值在0~32676之间!
如果需要扩大该范围,参考下面: #include <stdio.h> #include <stdlib.h> #include <time.h> unsigned long ulrand(void) { return ( (((unsigned long)rand()<<24)&0xFF000000ul) |(((unsigned long)rand()<<12)&0x00FFF000ul) |(((unsigned long)rand() )&0x00000FFFul)); } unsigned __int64 ullrand(void) { return ( (((unsigned __int64)ulrand())<<32) | ((unsigned __int64)ulrand())); } int i; unsigned long ul; unsigned __int64 ull; void main() { srand(time(NULL)); for (i=0;i<10;i++) { ul=ulrand(); printf("%010lu 0x%08x\n",ul,ul); } for (i=0;i<10;i++) { ull=ullrand(); printf("%020I64u 0x%016I64x\n",ull,ull); } } //3971076432 0xecb1d150 //2433428051 0x910b2a53 //1415415336 0x545d8628 //1312330759 0x4e389407 //1845758378 0x6e0409aa //0008069933 0x007b232d //4202720757 0xfa806df5 //2669855255 0x9f22c217 //0312068736 0x1299ca80 //2084555989 0x7c3fccd5 //03502077880857307931 0x3099e1472040ab1b //16884702393146816355 0xea52835e19b43763 //01877364819396611730 0x1a0dbd5b45f34e92 //07839440151924835771 0x6ccb4948756a05bb //09471412086917299176 0x8371371c820bfbe8 //04411255971577469925 0x3d37edef2f321be5 //13735846279546091130 0xbe9f876a65b7367a //04512980766520059820 0x3ea15418aa9927ac //15821377118299441610 0xdb90d2a9f1bb49ca //15512417228822200185 0xd7472d480398bf79 |