Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random number generator #9

Open
kumarbal opened this issue Jun 4, 2022 · 2 comments
Open

Random number generator #9

kumarbal opened this issue Jun 4, 2022 · 2 comments

Comments

@kumarbal
Copy link

kumarbal commented Jun 4, 2022

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
=#
@RGerzaguet
Copy link
Member

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 :)

@kumarbal
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants