By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 2,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

rand() implementation

P: n/a
I found some rand source code on this link:

http://www.codeguru.com/forum/showth...oto=nextnewest

void __cdecl srand (unsigned int seed)
{
#ifdef _MT
_getptd()->_holdrand = (unsigned long)seed;
#else /* _MT */
holdrand = (long)seed;
#endif /* _MT */
}

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 */
}

So I tried to re-create the rand() function like this on visual c/c++ 6:

int holdrandbla = 0;

int bla()
{
return(((holdrandbla = holdrandbla * 214013L + 2531011L) >> 16) & 0x7fff);
}
int main()
{
printf("blablabla started \n");

printf("blabla1: %d \n", rand() );
printf("blabla1: %d \n", bla() );

printf("blabla2: %d \n", rand() );
printf("blabla2: %d \n", bla() );

printf("blabla3: %d \n", rand() );
printf("blabla3: %d \n", bla() );
printf("blablabla finished \n");

return 0;
}

/// Output:

blablabla started
blabla1: 41
blabla1: 38
blabla2: 18467
blabla2: 7719
blabla3: 6334
blabla3: 21238
blablabla finished

Press any key to continue

As you can see the output is not the same...

I would like the output of bla() to be the same as rand() on visual c/c++
6.0 and/or 5.0

Can you fix my bla() function ?

Bye,
Skybuck.
Nov 14 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.