How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the jq programming language
How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the jq 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 jq programming language
Source code in the jq programming language
include "elementary-cellular-automaton" {search : "."};
# If using jq, the def of _nwise can be omitted.
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
# Input: an array of bits represented by 0s, 1s, "0"s, or "1"s
# Output: the corresponding decimal on the assumption that the leading bits are least significant,
# e.g. [0,1] => 2
def binary2number:
reduce (.[]|tonumber) as $x ({p:1}; .n += .p * $x | .p *= 2) | .n;
("1" + 100 * "0" ) | [automaton(30; 80) | .[0:1]] | [_nwise(8) | reverse | binary2number]
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Ol programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Lingo programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Rosetta Code/Find unimplemented tasks step by step in the Oz programming language