Rand Implementation

前端 未结 3 1584
南方客
南方客 2020-11-27 06:57

I would like to go through how rand() and srand() functions are implemented and would like to tweak the code to modify it to my requirements. Where can i find the source cod

3条回答
  •  独厮守ぢ
    2020-11-27 07:11

    It takes a seed as in input argument, usually like follows:-

    double result = srand(time(NULL));
    

    and returns a random number that adheres to the probability and hence expected number of occurrences.

    from CodeGuru forums:-

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

    Hope this helps.

提交回复
热议问题