Code Golf: MSM Random Number Generator

Posted by Vivin Paliath on Stack Overflow See other posts from Stack Overflow or by Vivin Paliath
Published on 2010-04-23T23:19:15Z Indexed on 2010/04/23 23:23 UTC
Read the original article Hit count: 606

The challenge

The shortest code by character count that will generate (pseudo)random numbers using the Middle-Square Method.

The Middle-Square Method of (pseudo)random number generation was first suggested by John Von Neumann in 1946 and is defined as follows:

Rn+1 = mid((Rn)2, m)

For example:

34562 = 11943936

mid(11943936) = 9439

94392 = 89094721

mid(89094721) = 0947

9472 = 896809

mid(896809) = 9680

96802 = 93702400

mid(93702400) = 7024

Test cases:

A seed of 8653 should give the following numbers (first 10):

8744, 4575, 9306, 6016, 1922, 6940, 1636, 6764, 7516, 4902

© Stack Overflow or respective owner

Related posts about code-golf

  • Code Golf: Collatz Conjecture

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple… >>> More

  • Code Golf - p day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character, followed by an approximation of p. Input is a single number, R. Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf - PI day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character. Followed by an approximation of pi Input is a single number, R Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf: Triforce

    as seen on Stack Overflow - Search for 'Stack Overflow'
    This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable. When the input is 1, print the following: *********** *********… >>> More

  • Code Golf: Tic Tac Toe

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player 1 (X) 2 = player 2 (O) So, given the array b… >>> More

Related posts about rosetta-stone