Skip to content

Random number generator #9

Open
@kumarbal

Description

@kumarbal

May be a good idea to keep random number generation predictable and independent. An example:

using Random

#=
Generate a stream of random bits.
Provide functions that convert a bit stream into a randomQAM symbol stream 
=#

struct BitGenerator 
    rng::MersenneTwister

    function BitGenerator(seed::UInt32)
        rng = MersenneTwister(seed);
        new(rng);
    end
end

function genBits(bgen::BitGenerator, nbits::Int64)
    buffer = rand(bgen.rng, [0,1], nbits);
end


function genBits!(bgen::BitGenerator, buffer::Vector{Int64})
    buffer = rand!(bgen.rng, buffer, [0,1]);
end

bgen = BitGenerator(UInt32(12))
buffer = zeros(Int64, 12)
genBits!(bgen, buffer)
#=
12-element Vector{Int64}:
 0
 1
 1
 1
 0
 ⋮
 1
 1
 1
 0
 0
=#

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions