How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Julia programming language
How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Julia 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 Julia programming language
This Julia code is a function called evolve
that simulates the evolution of a cellular automaton according to a given rule. Here's a detailed explanation of the code:
-
evolve(state, rule, N=64)
: This is the main function that takes three parameters:state
: The initial state of the cellular automaton, represented as a 64-bit integer (UInt64).rule
: The rule to be applied to evolve the cellular automaton, represented as a 32-bit integer (UInt32).N
: The number of cells in the cellular automaton. The default value is 64.
-
B(x) = UInt64(1) << x
: This creates a functionB
that takes an 8-bit integerx
and returns a 64-bit integer with a single bit set to 1 at thex
-th position. This function is used to create bitmasks for extracting individual bits from thestate
. -
The outer loop iterates over
p
from 0 to 9:- The purpose of this loop is to apply the rule to each bit in the
state
10 times.
- The purpose of this loop is to apply the rule to each bit in the
-
Inside the outer loop, there is an inner loop that iterates over
q
from 7 to 0:- This inner loop applies the rule to each of the 8 bits of the
state
, starting from the most significant bit.
- This inner loop applies the rule to each of the 8 bits of the
-
Within the inner loop:
st = UInt64(state)
: Converts thestate
from a 64-bit integer to an 8-bit integer.b |= (st & 1) << q
: Extracts theq
-th bit of thestate
and shifts it left byq
positions, then bitwise ORs it with the previous value ofb
.state = UInt64(0)
: Resets thestate
to 0.- The next loop iterates over each of the
N
cells in the cellular automaton:t1 = (i > 0) ? st >> (i - 1) : st >> (N - 1)
: Calculates the bit to the left of the current cell (if it exists).t2 = (i == 0) ? st << 1 : (i == 1) ? st << (N - 1) : st << (N + 1 - i)
: Calculates the bit to the right of the current cell (if it exists).if UInt64(rule) & B(7 & (t1 | t2)) != 0
: Checks if the rule specified by therule
parameter is satisfied for the current cell and its neighbors.UInt64(rule) & B(7 & (t1 | t2)) != 0
: This expression uses bitwise AND (&) and bitwise OR (|) to determine if the specified rule is satisfied. It checks if the bit at theq
-th position in therule
(obtained by7 & (t1 | t2)
) is set to 1.- If the rule is satisfied, it means that the cell will be set to 1 in the next generation.
state |= B(i)
: Sets thei
-th bit of thestate
to 1 if the rule is satisfied.
-
After applying the rule to all 8 bits and all
N
cells, the updatedstate
is printed as a binary bitmask. -
The function prints the evolved
state
for 10 generations.
To use this function, you would call it like this:
evolve(1, 30)
This would simulate the evolution of a cellular automaton with an initial state of 1 and a rule of 30 (also known as the Rule 30 cellular automaton) for 10 generations. The output would be a sequence of 10 binary bitmasks, each representing the state of the cellular automaton at that generation.
Source code in the julia programming language
function evolve(state, rule, N=64)
B(x) = UInt64(1) << x
for p in 0:9
b = UInt64(0)
for q in 7:-1:0
st = UInt64(state)
b |= (st & 1) << q
state = UInt64(0)
for i in 0:N-1
t1 = (i > 0) ? st >> (i - 1) : st >> (N - 1)
t2 = (i == 0) ? st << 1 : (i == 1) ? st << (N - 1) : st << (N + 1 - i)
if UInt64(rule) & B(7 & (t1 | t2)) != 0
state |= B(i)
end
end
end
print("$b ")
end
println()
end
evolve(1, 30)
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Zig programming language
You may also check:How to resolve the algorithm Additive primes step by step in the J programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element insertion step by step in the Pop11 programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Maple programming language
You may also check:How to resolve the algorithm De Polignac numbers step by step in the Euler programming language