From 4b262a0673b4a028bf6c4dee3adc7686e806b1f7 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Mon, 8 Aug 2016 22:21:27 -0500 Subject: [PATCH] Give the Random123 RNGs an easy option for seeding --- pyopencl/clrandom.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pyopencl/clrandom.py b/pyopencl/clrandom.py index 77a7e012..98e71b49 100644 --- a/pyopencl/clrandom.py +++ b/pyopencl/clrandom.py @@ -419,16 +419,25 @@ class Random123GeneratorBase(object): .. automethod:: normal """ - def __init__(self, context, key=None, counter=None): + def __init__(self, context, key=None, counter=None, seed=None): int32_info = np.iinfo(np.int32) + from random import Random + + rng = Random(seed) + + if key is not None and counter is not None and seed is not None: + raise TypeError("seed is unused and may not be specified " + "if both counter and key are given") if key is None: - from random import randrange - key = [randrange(int(int32_info.min), int(int32_info.max)+1) + key = [ + rng.randrange( + int(int32_info.min), int(int32_info.max)+1) for i in range(self.key_length-1)] if counter is None: - from random import randrange - counter = [randrange(int(int32_info.min), int(int32_info.max)+1) + counter = [ + rng.randrange( + int(int32_info.min), int(int32_info.max)+1) for i in range(4)] self.context = context -- GitLab