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!

  • 4
    If I may: it takes a lot of care and effort to write a *reliable* PRNG that passes all the usual statistical tests (no correlations, sufficiently long period, etc.). Why not use a canned method and be done with it? But if you absolutely must: try looking up [linear congruential](http://en.wikipedia.org/wiki/Linear_congruential_generator) or [lagged Fibonacci](http://en.wikipedia.org/wiki/Lagged_Fibonacci_generator) generators, and see if you can adapt them for "mental" use.2011-10-03
  • 0
    It's not clear to me how you're using "pseudo-random" here. It seems that by "two integer inputs (one < 30, one < 15)" you mean positive, or at least non-negative, integer inputs, so you have at most $450$ different argument combinations. In what sense could a mapping of these combinations to the numbers from $1$ to $50$ be called "pseudo-random"?2011-10-03
  • 0
    by pseudo-random I mean: not obvious what the mapping formula is2011-10-03
  • 0
    @J.M. it's meant for a mentalism performance type of thing, which is why I added the "mental" use ;-) I think the linear congruential is the easiest of the two, so thanks for the information!2011-10-03
  • 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.

  • 2
    Taking the square of a four-digit number (even if it's just the two middle digits) is not exactly what I'd call "easy to compute" :-)2011-10-03
  • 0
    @joriki, did the OP say that he wanted to do it in his head?2011-10-03
  • 2
    Well, that's a philosophical question. It says "it should be easy to do mentally". Unless you subscribe to some form of mind-body dualism, that would mean to do it "in his head". :-)2011-10-03
  • 0
    @joriki, I missed "mentally"!2011-10-03