Skip to content

Commit

Permalink
blocks/sources/uniformrandom: fix seed initialization
Browse files Browse the repository at this point in the history
The initialisation of the random seed has been moved from
UniformRandomSource:initialize() to UniformRandomSource:process() in
order to fix the problem of math.random() generating the same sequence
in all the processes when the math.randomseed() action is done before
process forking.

Signed-off-by: Vanya A. Sergeev <v@sergeev.io>
  • Loading branch information
FAPMon authored and vsergeev committed Mar 29, 2024
1 parent 4ca9c5e commit dbcf8cd
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions radio/blocks/sources/uniformrandom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,15 @@ function UniformRandomSource:get_rate()
end

function UniformRandomSource:initialize()
if self.seed then
math.randomseed(self.seed)
end

self.out = self.data_type.vector(self.chunk_size)
end

function UniformRandomSource:process()
local out = self.out

-- Only run once in each block instance, as math.randomseed() returns nil
self.seed = self.seed and math.randomseed(self.seed)

for i=0, out.length-1 do
out.data[i] = self.generator()
end
Expand Down

0 comments on commit dbcf8cd

Please sign in to comment.