How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Wren programming language
How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Wren programming language
Table of Contents
Problem Statement
Rule 30 is considered to be chaotic enough to generate good pseudo-random numbers. As a matter of fact, for a long time rule 30 was used by the Mathematica software for its default random number generator. Steven Wolfram's recommendation for random number generation from rule 30 consists in extracting successive bits in a fixed position in the array of cells, as the automaton changes state. The purpose of this task is to demonstrate this. With the code written in the parent task, which you don't need to re-write here, show the ten first bytes that emerge from this recommendation. To be precise, you will start with a state of all cells but one equal to zero, and you'll follow the evolution of the particular cell whose state was initially one. Then you'll regroup those bits by packets of eight, reconstituting bytes with the first bit being the most significant. You can pick which ever length you want for the initial array but it should be visible in the code so that your output can be reproduced with an other language. For extra-credits, you will make this algorithm run as fast as possible in your language, for instance with an extensive use of bitwise logic.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Wren programming language
Source code in the wren programming language
import "./big" for BigInt
var n = 64
var pow2 = Fn.new { |x| BigInt.one << x }
var evolve = Fn.new { |state, rule|
for (p in 0..9) {
var b = BigInt.zero
for (q in 7..0) {
var st = state.copy()
b = b | ((st & 1) << q)
state = BigInt.zero
for (i in 0...n) {
var t1 = (i > 0) ? st >> (i-1) : st >> 63
var t2 = (i == 0) ? st << 1 : (i == 1) ? st << 63 : st << (n+1-i)
var t3 = (t1 | t2) & 7
if ((pow2.call(t3) & rule) != BigInt.zero) state = state | pow2.call(i)
}
}
System.write(" %(b)")
}
System.print()
}
evolve.call(BigInt.one, 30)
You may also check:How to resolve the algorithm Primality by trial division step by step in the Quackery programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Lua programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Tcl programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Quackery programming language
You may also check:How to resolve the algorithm Documentation step by step in the PARI/GP programming language