Percolation/Mean cluster density: Difference between revisions

m
m (Altered code to be more C++ idiomatic.)
 
(3 intermediate revisions by 2 users not shown)
Line 40:
R (0 .< n).map(i -> (0 .< @n).map(i -> Int(nonrandom() < @@p)))
 
F walkMaze(&grid, m, n, idx) -> NVoid
grid[n][m] = idx
I n < grid.len - 1 & grid[n + 1][m] == NotClustered
Line 224:
#include <string>
#include <vector>
#include <iomanip>
 
std::random_device random;
Line 245 ⟶ 246:
 
void display() const {
for ( uint64_t row = 0; row < grid.size(); row++row ) {
for ( uint64_t col = 0; col < grid.size(); col++col ) {
uint64_t value = grid[row][col];
char ch = ( value < GRID_CHARACTERS.length() ) ? GRID_CHARACTERS[value] : '?';
Line 257 ⟶ 258:
void count_clusters() {
clusters = 0;
for ( uint64_t row = 0; row < grid.size(); row++row ) {
for ( uint64_t col = 0; col < grid.size(); col++col ) {
if ( grid[row][col] == CLUSTERED ) {
clusters += 1;
Line 285 ⟶ 286:
void create_grid(int32_t grid_size, double probability) {
grid.assign(grid_size, std::vector<int32_t>(grid_size, 0));
for ( int32_t row = 0; row < grid_size; row++row ) {
for ( int32_t col = 0; col < grid_size; col++col ) {
if ( distribution(generator) < probability ) {
grid[row][col] = CLUSTERED;
Line 307 ⟶ 308:
 
Grid grid(size, probability);
printf("%s%d%s%d%s%d%s%s",std::cout << "This ", << size, << " by ", << size, << " grid contains ", grid.cluster_count(), " clusters:", "\n");
<< grid.cluster_count() << " clusters:" << std::endl;
grid.display();
 
printf("%s%s%d%s",std::cout << "\n", " p = 0.5, iterations = ", << test_count, "\n")<< std::endl;
std::vector<int32_t> gridSizesgrid_sizes = { 10, 100, 1'000, 10'000 };
for ( int32_t gridSizegrid_size : gridSizesgrid_sizes ) {
double sumDensity = 0.0;
for ( int32_t test = 0; test < test_count; test++ ) {
Grid grid(gridSizegrid_size, probability);
sumDensity += grid.cluster_density();
}
double result = sumDensity / test_count;
std::cout << " n = " << std::setw(5) << grid_size
printf("%s%5d%s%.6f%s", " n = ", gridSize, ", simulation K = ", result, "\n");
<< ", simulations K = " << std::fixed << result << std::endl;
}
}
Line 2,053 ⟶ 2,056:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
1,480

edits