rand() implementation

S

Skybuck Flying

I found some rand source code on this link:

http://www.codeguru.com/forum/showthread.php?t=312416&goto=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.
 
T

Thomas Stegen

Skybuck said:
I found some rand source code on this link:

http://www.codeguru.com/forum/showthread.php?t=312416&goto=nextnewest

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

Can you fix my bla() function ?


Maybe you need to call srand with an argument of 0?

At least you need to make sure that both functions start at the same
place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top