DigitalComm.jl icon indicating copy to clipboard operation
DigitalComm.jl copied to clipboard

Random number generator

Open kumarbal opened this issue 3 years ago • 2 comments

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
=#

kumarbal avatar Jun 04 '22 03:06 kumarbal

Hello, Thanks for the feedback ! I am not really sure to understand your point. To have a bit stream generation in DigitalComm you have genBitSequence that takes a size N as input parameter and a seed, so it seems to be similar with what you point ? The seed is an optional parameter though, but this seems fine for me

In any ways we have:

using DigitalComm, Random
N = 512 
seed = 1234
bitSeq = genBitSequence(N,seed)

bitSeq2 = genBitSequence(N,seed)
all(bitSeq .== bitSeq2) # returns true 

But if I have understand at all what was your point, do not hesitate to tell :)

RGerzaguet avatar Jun 09 '22 19:06 RGerzaguet

Hello

Thanks for replying. Maybe I have misunderstood whether the use of a seed within 'genBitSequence' will retain state on successive calls to that routine.

In the example I give above, the seed can be set initially and the state is maintained as per the last call so that successive calls progress the random number generator state. This means I can have several generators in the program, each able to create independent random streams that can be called within a loop.

Hope that explanation helps.

Kind regards Kumar

kumarbal avatar Jun 11 '22 21:06 kumarbal