How to resolve the algorithm Pick random element step by step in the Zig programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pick random element step by step in the Zig programming language

Table of Contents

Problem Statement

Demonstrate how to pick a random element from a list.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pick random element step by step in the Zig programming language

Source code in the zig programming language

const std = @import("std");

const debug = std.debug;
const rand = std.rand;
const time = std.time;

test "pick random element" {
    var pcg = rand.Pcg.init(time.milliTimestamp());

    const chars = [_]u8{
        'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H',
        'I', 'J', 'K', 'L',
        'M', 'N', 'O', 'P',
        'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X',
        'Y', 'Z', '?', '!',
        '<', '>', '(', ')',
    };

    var i: usize = 0;
    while (i < 32) : (i += 1) {
        if (i % 4 == 0) {
            debug.warn("\n  ", .{});
        }
        debug.warn("'{c}', ", .{chars[pcg.random.int(usize) % chars.len]});
    }

    debug.warn("\n", .{});
}


  

You may also check:How to resolve the algorithm Holidays related to Easter step by step in the jq programming language
You may also check:How to resolve the algorithm Deming's funnel step by step in the Raku programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Variables step by step in the Set lang programming language