Re: simple PRNG?
In article <fs057r$7rd$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
On a system where int was 16 bit, random()&0xffff
risks exceeding INT_MAX, so printing the result with a %d format
could result in negative numbers, such as would occur for
the results 44249 46978 56008. An unsigned output format
should be used instead.
--
"Prevention is the daughter of intelligence."
-- Sir Walter Raleigh
In article <fs057r$7rd$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
>Richard Heathfield wrote:
>jacob navia said:
>>Richard Heathfield wrote:
>>>jacob navia said:
>>>jacob navia said:
>>>>This needs 32 bit maths as you see. I think it could be ported to 16
>>>>bits by making the result mod 2^16 -1
>>>>bits by making the result mod 2^16 -1
>This program:
>---------------------------------------------------------------
>/*
* Pseudo-random number generator. The result is uniform on
* [0, 2^31 - 1].
*/
>---------------------------------------------------------------
>/*
* Pseudo-random number generator. The result is uniform on
* [0, 2^31 - 1].
*/
>#include <stdio.h>
>int main(void)
>{
> for (int i=0; i<10;i++) {
> printf("%d\n",r andom()&0xffff) ;
> }
>}
>int main(void)
>{
> for (int i=0; i<10;i++) {
> printf("%d\n",r andom()&0xffff) ;
> }
>}
>-----------------------------------------------------------------------
>produces the following output:
>16807
>15089
>44249
>3114
>46978
>56008
>produces the following output:
>16807
>15089
>44249
>3114
>46978
>56008
risks exceeding INT_MAX, so printing the result with a %d format
could result in negative numbers, such as would occur for
the results 44249 46978 56008. An unsigned output format
should be used instead.
--
"Prevention is the daughter of intelligence."
-- Sir Walter Raleigh
Comment