472,782 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 6309
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.