473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 6390
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Protoman | last post by:
I need to write a cryptographically secure pseudorandom number generator for a crypto app I'm writing; if you could give me some example code, that'd be great. Thanks for the help!!!
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
31
by: pinkfloydhomer | last post by:
Using rand() in and old version og gcc, and using Tausworth's method, I calculated the frequency of 0 or 1 in the first digit like this: int hist = {0,0}; for (i = 0; i < 100000; ++i) {...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.