468,463 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

simple PRNG?

Can anyone point me to a simple, fast RRNG function to generate random ints
within a specified range? It is important that each value within the range
has the same probability (uniform distribution).
I do not want to use the unreliable rand() function, but I do not want to
bloat my code with something as complex as MT either. I am just looking for
a short code snippet that I can copy without worrying about licensing.
The function should work on limited platforms (no floating-point math
please, one that works even on platforms where int is only 16 bit would be
perfect).
I did search this group and the web but I could not find anything which
meets the requirements.


Mar 21 '08
55 5875
CBFalconer wrote:
>
pete wrote:
... snip ...

Why are there any signed type objects at all
in that function definition?

register long unsigned fixes everything.

But I wouldn't publish purportedly portable code
with the keyword "register" in it, either.

Why not?
Because, ...
register is still a reserved word. All it is guaranteed
to do is prevent taking the address. Although it seems totally
ridiculous here.
.... I can't imagine that there ever might be
a situation where I would want to prevent taking the address.

--
pete
Mar 29 '08 #51
In article <47***********@mindspring.com>,
pete <pf*****@mindspring.comwrote:
>CBFalconer wrote:
>register is still a reserved word. All it is guaranteed
to do is prevent taking the address. Although it seems totally
ridiculous here.
>... I can't imagine that there ever might be
a situation where I would want to prevent taking the address.
It becomes an optimization hint to a compiler: if there are
pointers in the code, the optimizer can know that none of them
point to the object declared as 'register', which fact might
allow it to use tighter code in some cases.
--
"Eightly percent of the people in the world are fools and the
rest of us are in danger of contamination." -- Walter Matthau
Mar 29 '08 #52
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <47***********@mindspring.com>,
pete <pf*****@mindspring.comwrote:
>>CBFalconer wrote:
>>register is still a reserved word. All it is guaranteed
to do is prevent taking the address. Although it seems totally
ridiculous here.
>>... I can't imagine that there ever might be
a situation where I would want to prevent taking the address.

It becomes an optimization hint to a compiler: if there are
pointers in the code, the optimizer can know that none of them
point to the object declared as 'register', which fact might
allow it to use tighter code in some cases.
On the other hand, since "register" can only be applied an object
declared inside a function, an optimizing compiler probably already
knows that the object's address is never taken.

On the other other hand, the code in question was published in 1988;
at the time, "register" might well have been useful as an optimization
hint.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 29 '08 #53
Walter Roberson wrote:
>
In article <47***********@mindspring.com>,
pete <pf*****@mindspring.comwrote:
CBFalconer wrote:
register is still a reserved word. All it is guaranteed
to do is prevent taking the address. Although it seems totally
ridiculous here.
... I can't imagine that there ever might be
a situation where I would want to prevent taking the address.

It becomes an optimization hint to a compiler: if there are
pointers in the code, the optimizer can know that none of them
point to the object declared as 'register', which fact might
allow it to use tighter code in some cases.
Is that something that you've done for that reason,
or is that an example of something that you can imagine
that I can't?

--
pete
Mar 29 '08 #54
In article <fs**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>It becomes an optimization hint to a compiler: if there are
pointers in the code, the optimizer can know that none of them
point to the object declared as 'register'
I would have thought most compilers would be able to tell whether
the address of a variable is taken, so that this hint doesn't
provide any information they don't already have.

-- Richard
--
:wq
Mar 29 '08 #55
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <47***********@mindspring.com>,
pete <pf*****@mindspring.comwrote:
>>CBFalconer wrote:
>>register is still a reserved word. All it is guaranteed
to do is prevent taking the address. Although it seems totally
ridiculous here.
>>... I can't imagine that there ever might be
a situation where I would want to prevent taking the address.

It becomes an optimization hint to a compiler: if there are
pointers in the code, the optimizer can know that none of them
point to the object declared as 'register', which fact might
allow it to use tighter code in some cases.
Sounds strange.

Mar 29 '08 #56

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.