# Need algorithm: gen Pseudo Random Numbers - EXCEPT!

I need an algorithm to generate Pseudo Random Number Sequences - EXCEPT ... the algorithm must only use generic arithmetic functionality available in most programming languages.

Why? Because I need to write code in C#, Android, C++, Objective-C ... such that, if given the same beginning seed all languages will generate the exact same pseudo random sequence of results.

Any one got any idea's?
LVL 5
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
http://mathoverflow.net/questions/29494/pseudo-random-number-generation-algorithms

static unsigned long
x=123456789,y=362436069,z=521288629,w=88675123,v=886756453;
/* replace defaults with five random seed values in calling program */
unsigned long xorshift(void)
{unsigned long t;
t=(x^(x>>7)); x=y; y=z; z=w; w=v;
v=(v^(v<<6))^(t^(t<<13)); return (y+y+1)*v;}
0
IT Business Systems Analyst / Software DeveloperCommented:
Most of those languages would have the mathematic constructs to implement many of the PRNG algorithms around. However, if you are after a relatively simple algorithm so that it is easier for YOU to implement, than I would probably be looking at a Linear Feedback Shift Register algorithm. Even Wiki has probably enough info to get you going...

https://en.wikipedia.org/wiki/Linear_feedback_shift_register

All that is required is generic bit manipulations, such as bit shifting, XOR, AND, negation.... should be no problem in any language you are required to use.
0
Author Commented:
@MajorBigDeal - I do not think Bit-Shifting (e.g. << and >>) will be a generic arithmetic solution. I could be wrong, maybe you can write four code snippets using Bit-Shifting?

(1) C#:
(2) Android:
(3) C++:
(4) Objective-C:

Fred
0
Author Commented:
@mccarl - ROFL ... what you don't know is ... I'm not smart enough to understand the formulas in most Wiki articles ... sigh ...

I know, I should have taken more math in school, but crafts and drama were soooo much more fun.

Fred
0
IT Business Systems Analyst / Software DeveloperCommented:
All languages use EXACTLY the same syntax, ie. << and >> for bit shifting (BTW, quite easy to find that via Google)
0
IT Business Systems Analyst / Software DeveloperCommented:
The Wiki article that I linked gives you actual code samples!! And as you can see in general it boils down to pretty much a one liner.
0
Author Commented:
@mccarl

P.S. would sure be nice if I could find a job spewing Shakespeare or making pot-holders.

aaaaaarrrrrgggghhhh!
0
Author Commented:
@mccarl - okay I'll try using this one - any comments before I jump in? (I'll start with C# since that's one I use often)

``````#include <stdint.h>
int main(void)
{
uint32_t lfsr = 1;
unsigned period = 0;

do
{
/* taps: 32 31 29 1; feedback polynomial: x^32 + x^31 + x^29 + x + 1 */
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xD0000001u);
++period;
} while(lfsr != 1u);

return 0;
}
``````
0
IT Business Systems Analyst / Software DeveloperCommented:
No, that is the code snippet that I would be looking at if I was doing the same as you.

I hope I am not stating the obvious, but do you realise that only line 10 of the above is all YOU need!? The rest of that code is used for checking the "period" of the LFSR.
0
Commented:
Bit shifting is very generic but no I can't give you "Android" or Objective-C code because I don't know how.  8*)
0
Commented:
The following pseudo-code snippet will generate a 1000 random values between 0 and 1 for the variable rand. The sequence is determined by the the initial value of seed. The code is easily modified to produce numbers within any range, including integers within a range. This code assumes 16 digit integer representation.

``````seed=15239;
for (i=0;i<1000;i++){
seed =(86028157*seed + 142501)%1299689;
rand=seed/1299689;
print(rand);
}
``````
Or the following if using type int.
``````seed=43;
for (i=0;i<1000;i++){
seed =(5171*seed + 142501)%34421
rand=seed/34421;
print(rand);
}
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Any well specified PRNG algorithm would produce the same result regardless of implementation (assuming that it was implemented correctly)
Some more sophisticated algorithms may be difficult to implement on platforms that don't provide support for high level languages or large number arithmetic.
If you are looking for simple algorithms and are willing to sacrifice quality of the random sequence generated, we should ask how far you want to go.
Returning a constant 0 every time is a possible, though unlikely result of a truly random sequence, and should be easy to implement on any platform.
Maybe you want to minimally specify the range of numbers to be returned, and to stipulate that each possible value is returned the same number of times over a specified cycle length?
It would also help to know the maximum size of numbers on which it is easy to perform arithmetic on the minimal platform of which you are interested.
0
Author Commented:
@GwenForWeb
That was EXACTLY what I needed. Thanks.

Everyone got points, but Gwen got more.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.