What is returned ?

S

Skybuck Flying

Hi,

I have a C question.

Let's assume MT is not defined

int holdrand = 1;

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

I have trouble understanding what this function returns.

My confusion is with this line:

return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);

Apperently this line assigns some value to holdrand.

Something like:

holdrand becomes holdrand * blablabla etc.

But what is returned ?

I think it returns holdrand after the calculation.

But.... if that would be true... then this random number generator simply
uses the previous rand value to calculate the next one ?

Weird... I don't understand it... so that's why I ask this newsgroup =D

Bye,
Skybuck
 
A

Andrey Tarasevich

Skybuck said:
...
My confusion is with this line:

return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);

Apperently this line assigns some value to holdrand.

This is equivalent to

holdrand = holdrand * 214013L + 2531011L;
return (holdrand >> 16) & 0x7fff;
Something like:

holdrand becomes holdrand * blablabla etc.

Yes, 'holdrand' becomes 'holdrand * 214013L + 2531011L'.
But what is returned ?

I think it returns holdrand after the calculation.

No. It returns '(holdrand >> 16) & 0x7fff', where 'holdrand' holds the
new value, i.e. value assigned by the inner assignment operator.
But.... if that would be true... then this random number generator simply
uses the previous rand value to calculate the next one ?

Of course. That's how oracle-type pseudo-random number generators work.
They are stateful functions, which means that they generate the new
state based on the previous state.
Weird... I don't understand it... so that's why I ask this newsgroup =D

What exactly don't you understand?
 

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,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top