3
$\begingroup$

I'm trying to create a function that takes two integer inputs (one < 30, one < 15), and which creates a pseudo-random value between 1 and 50.

My first attempt is something like this:

  • sum the two numbers
  • flip the digits around (with 2 becoming 20)
  • if new number > 50, do -50

(I've fudged the numbers a little for the example, but that's my general idea.)

The "randomness" I introduce is of course the flipping of digits (subquestion: how to write down this easy operation for pattern analysts like humans, but difficult to turn into math?), but my entire method has the unfortunate effect that the summing of the integers ensures that some sums are reached more often than others.

I know I can just make the sum and divide by the max to get an equal spread, but it's not really random (as in: it's very predictable). Hence the flipping.

Do you know if there's another way to combine two integers, and return a pseudo-random integer? It doesn't have to be unique (which would be impossible), and it should be easy to do mentally.

Cheers!

  • 0
    I read your comment by chance (pseudo-randomly? :-); you need to use the @ if you want someone to be notified.2011-10-03

3 Answers 3

3

If you just want the result not to be obvious, you could do this: Memorize some arbitrary permutation of the digits from $0$ to $9$. (If you don't want to memorize that much, you can use a permutation of the digits from $0$ to $4$ and if necessary subtract and add $5$ before and after applying it.) Then just take $30$ times the number $\lt15$, plus the number $\lt30$, take the last two digits of the result, permute them both according to the memorized permutation, and if necessary add or subtract $50$ to bring the result into the desired range from $1$ to $50$. This will map $9$ of the $450$ input combinations to each of the $50$ output values (assuming the admissible inputs start at $0$), and the result should appear arbitrary at first sight.

2

At each iteration, let $p$ denote the previous random number, and $a$, $b$ new random numbers from your two generators. Set $p = r=(p+a+b)\mod 50$. Here's an example of results:

 r= 28, 2, 38, 29, 37, 9, 38, 14, 38, 46, 10, 36, 13,    46, 14, 40, 1, 17, 36, 42, 15, 24, 4, 40, 17, 3, 26,    3, 14, 33, 0, 33, 42, 48, 12, 42, 22, 40, 9, 23 ...  

In numeric tests with a C program, after 100,000 numbers, 41 of 50 histogram cells showed 2% of occupancy, 6 showed 1.9%, and 3 showed 2.1%. After a million numbers, all cells showed 2% occupancy. Thus, long-term uniformity appears to be acceptable. Short-term correlation is slightly elevated because the sum of two uniform random number generators is not uniform; for example, if your two generators are uniform, numbers from 15 to 30 occur about 50% more often than they would for a uniform distribution.

0

Concatenate the decimal representations of the two numbers $a,b$ into say $x=100a+b$ and take the middle digits of the $x^2$, mod $50$ if necessary. This is a version of von Neumann's Middle-square method. This is not a particularly good method when it comes to randomness but it is easy to compute.

  • 0
    @joriki, I missed "mentally"!2011-10-03