The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

[SOLVED] Generating random numbers using only grade-school math

whuppinswhuppins Registered User regular
edited June 2007 in Help / Advice Forum
I'm working in some software that includes a very basic library of math functions. I need to build a passable random number generator from these functions with as few manipulations as possible. If you're wondering what "passable" is, well, the bar has actually been set a lot lower than most RNGs. There will be no QC done on the results; it's just about as informal as you can get for something like this. Not too bad, so far, but it gets difficult when you consider the limitations on the tools I have to build this algorithm.

Seeding
The RNG must unfortunately be seeded by some pretty mundane data. Pulling two 6-digit integers is about as sophisticated as I can get. There is a system time/date function but it's only precise to whole seconds, and I can't convert it to a numeric string to be manipulated anyway. The two integers aren't even random themselves; one is basically a serial number that will increment by 1-5 or so with each iteration, and one is a different type of serial that won't be quite as predictable but a group of 20 samples will generally fall within the same range of, say, 50 or so.

Math
The software can perform some modest calculations. Here's a summary of the functions I have at my disposal:

Absolute value
Min/Max
Sin/Cos/Tan
Log10/Natural log/Exp (though it seems to just use e as a 5-digit decimal for these calculations)
Polynomials (not sure how it works but it looks pretty limited)
Rounding/modulo (remainders)
Square root
...Plus all the basic arithmetic functions, including factorials and exponentials.

Other limitations
I can't find any documentation, but I'm not trusting this software to be able to keep track of any more than 12 significant figures. Also, it refuses to use string functions on numeric values and vice versa, no matter how many times you multiply things by 1. So, while you can get the middle 2 digits of a 10-digit number by using mod and round functions, you can't use the "mid(5,2)" function like you would be able to do with text. In short, everything has to kept strictly numeric. Finally, I can't do any fancy iterative stuff, as each number must be generated in complete isolation from any other number in the set.

Validation
Despite all the above handicaps, the one break I get is that, as I mentioned, the numbers don't have to be very random at all in the rigorous sense. Here's how I'll test it:

1.) Use the algorithm to generate 100 integers between 0 and 100 (or whatever, that detail doesn't really matter) based on the following seeding pattern: A couple 6-digit seeds will be chosen and one will be incremented for each trial while the other stays the same.
Seed 1 Seed 2
456789 543210
456789 543211
456789 543212
...

2.) Print out the list of numbers and hand it to our lab director, who is a pharmacokinetics PhD but not the Rain Man or anything.

3.) Let him eyeball it for a couple minutes and see if he notices any patterns emerge. If the numbers seem random, then that's good enough.

I've tried a few things so far, but I guess I'm not clever enough to come up with an algorithm that avoids, I dunno, every third number incrementing by a linearly decreasing amount, or something like that. There's always a subtle pattern that the layman (me) can perceive. I need something a little stronger without performing like a dozen of the above calculations. Anyone have a reasonably elegant solution?

whuppins on

Posts

  • PheezerPheezer Registered User, ClubPA regular
    edited May 2007
    What is the data actually going to be used for?

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • KingMooKingMoo Registered User regular
    edited May 2007
    I always thought that every electronic RNG had a pattern to it or was not truly random. i could be wrong

    EDIT: a quick search in wikipedia brings up tons of RNG info

    KingMoo on
    ![▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓]!
    !!!!▓▓▓▓▓Gravy?▓▓▓▓▓!!!!!!
    !!!!!!▓▓▓▓▓▓▓▓▓▓▓▓▓!!!!!!!!!
    of doom
  • PheezerPheezer Registered User, ClubPA regular
    edited May 2007
    It sort of depends on the way it's seeded and so forth, but I'm pretty sure that if the same system that produces the seed also produces the "random" number, then yes, it won't be truly random. There are varying layers of subtlety to be had though, and a good enough not-random can often do just fine in lieu of true random.

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • ElJeffeElJeffe Registered User, ClubPA regular
    edited May 2007
    It sounds like the second set of "seed" numbers at your disposal is fairly non-deterministic. Could you take the sine (or cosine) of that, and then take, say, the 5th and 6th decimal places of that as your random number between 1 and 100?

    I know squat about RNGs, btw.

    ElJeffe on
    I submitted an entry to Lego Ideas, and if 10,000 people support me, it'll be turned into an actual Lego set!If you'd like to see and support my submission, follow this link.
  • Dance CommanderDance Commander Registered User regular
    edited June 2007
    The guy below me has much better suggestions, I forgot something really fundamental as far as psuedo-random numbers go, so I'm trashing my post!

    Dance Commander on
  • ClipseClipse Registered User regular
    edited June 2007
    Mersenne Twister is good for most applications (though not cryptography), and relatively easy to implement. Linear Congruential Method is probably the easiest to implement, but depending on how anal your lab director is, might not pass the test.

    There is also a method originally described by von Neumann which should be easy to implement with what you've got. Basically, you define Xi+1 as the "middle" digits of Xi^2. So, for instance, if your seed is 123456, then you define X1 as the middle digits of 15241383936, so maybe 41383, or 2413839. Obviously, "middle" can be defined in a variety of ways - I forget if there is a specific definition used in this method normally.

    Clipse on
  • AbelsAbels Registered User regular
    edited June 2007
    From K&R:
    unsigned long int next = 1;
    
    /* rand: return pseudo-random integer on 0..32767 */
    int rand(void)
    {
        next = next * 1103515245 + 12345;
        return (unsigned int)(next/65536) % 32768
    }
    
    /* srand: set seed for rand() */
    void srand(unsigned int seed)
    {
        next = seed;
    }
    

    Should be simple enough - only arithmatic.

    Abels on
  • whuppinswhuppins Registered User regular
    edited June 2007
    I should have known there would be an entire Wikipedia section on RNG algorithms. I appreciate all the replies, and consider this one solved.

    Oh yeah, and the numbers will be used to assign random test suites to batches of samples. It's an analytical lab, like CSI only our rooms are well-lit and none of us are snappy dressers.

    whuppins on
This discussion has been closed.