How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the C++ programming language
How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the C++ 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 C++ programming language
This code implements a cellular automaton, specifically rule 30.
A cellular automaton is a system of cells that interact with each other according to a set of rules.
In this case, each cell is a bit, and the rules are defined by the RULE
constant.
The evolve
function updates the state of the cellular automaton by applying the rules to each cell.
The show
function prints the state of the cellular automaton as a string of characters, with '#' representing a live cell and ' ' representing a dead cell.
The byte
function converts the state of the cellular automaton into a byte, which is an 8-bit value.
The main
function initializes the cellular automaton with a single live cell and then evolves the cellular automaton for 10 generations.
It then prints the state of the cellular automaton as a string of bytes.
Source code in the cpp programming language
#include <bitset>
#include <stdio.h>
#define SIZE 80
#define RULE 30
#define RULE_TEST(x) (RULE & 1 << (7 & (x)))
void evolve(std::bitset<SIZE> &s) {
int i;
std::bitset<SIZE> t(0);
t[SIZE-1] = RULE_TEST( s[0] << 2 | s[SIZE-1] << 1 | s[SIZE-2] );
t[ 0] = RULE_TEST( s[1] << 2 | s[ 0] << 1 | s[SIZE-1] );
for (i = 1; i < SIZE-1; i++)
t[i] = RULE_TEST( s[i+1] << 2 | s[i] << 1 | s[i-1] );
for (i = 0; i < SIZE; i++) s[i] = t[i];
}
void show(std::bitset<SIZE> s) {
int i;
for (i = SIZE; i--; ) printf("%c", s[i] ? '#' : ' ');
printf("|\n");
}
unsigned char byte(std::bitset<SIZE> &s) {
unsigned char b = 0;
int i;
for (i=8; i--; ) {
b |= s[0] << i;
evolve(s);
}
return b;
}
int main() {
int i;
std::bitset<SIZE> state(1);
for (i=10; i--; )
printf("%u%c", byte(state), i ? ' ' : '\n');
return 0;
}
You may also check:How to resolve the algorithm Input loop step by step in the Sidef programming language
You may also check:How to resolve the algorithm Convex hull step by step in the Ruby programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Rust programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Nanoquery programming language