Solved

Need algorithm: gen Pseudo Random Numbers - EXCEPT!

Posted on 2013-06-04
13
587 Views
Last Modified: 2013-06-05
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?
0
Comment
Question by:volking
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 11

Assisted Solution

by:MajorBigDeal
MajorBigDeal earned 60 total points
ID: 39220840
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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 120 total points
ID: 39220856
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
 
LVL 5

Author Comment

by:volking
ID: 39220859
@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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Author Comment

by:volking
ID: 39220867
@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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 120 total points
ID: 39220870
All languages use EXACTLY the same syntax, ie. << and >> for bit shifting (BTW, quite easy to find that via Google)
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 120 total points
ID: 39220873
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
 
LVL 5

Author Comment

by:volking
ID: 39220874
@mccarl

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

aaaaaarrrrrgggghhhh!
0
 
LVL 5

Author Comment

by:volking
ID: 39220888
@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;
}

Open in new window

0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 120 total points
ID: 39220897
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
 
LVL 11

Assisted Solution

by:MajorBigDeal
MajorBigDeal earned 60 total points
ID: 39220898
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
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 290 total points
ID: 39221059
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);
}

Open in new window

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);
}

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 30 total points
ID: 39221974
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
 
LVL 5

Author Closing Comment

by:volking
ID: 39223874
@GwenForWeb
That was EXACTLY what I needed. Thanks.

Everyone got points, but Gwen got more.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question