Random ClassΒΆ
This class implements functionalities associated with random number generation
and pdf sampling. This class can sample and fill ranges with data corresponding
to Gaussian
, Exponential
, Uniform
and Breit-Wigner
distributions.
This class is a wrapper of hydra C++ Random class
. The Random class have
two constructors to instantiate the Random class:
- Constructor with empty seed. The default seed value is
7895123
. - Constructor expecting the seed.
import HydraPython as hp
r = hp.Random() # default seed
r2 = hp.Random(8385977) # Seed value
# This will construct the 2 Random class's object one with default seed and
# one with the seed value 8385977
Apart from setting the seed value at the time of creation the seed can be
set or get with setter and getter methods named SetSeed
and GetSeed
.
r = hp.Random()
print (r.GetSeed()) # Give the seed value of object r. 7895123
r.SetSeed(988763) # New seed is 988763
print (r.GetSeed()) # 988763
The Random class provides a method named Uniform
to generate the numbers
between range (min, max) (both min and max exclusive) and
fill them into the container according to the Continuous Uniform
distribution.
The container can be any of the host_vector_float
or device_vector_float
.
In below examples, I have used device_vector_float extensively but they both
can be used interchangeably in place of each other.
import HydraPython as hp
container = hp.device_vector_float(1000000) # Continer to hold 1000000 objects
r = hp.Random() # Random number generator object
r.Uniform(1.1, 1.5, container) # Minimum number 1.1, maximum 1.5 and container
# Above will generate 1000000 numbers between (1.1, 1.5)
print (container[:10])
The Gauss random number generation method can also be used with the Random class.
The Gauss
method generate the numbers with the given mean
and sigma
values.
import HydraPython as hp
container = hp.device_vector_float(1000000) # Continer to hold 1000000 objects
r = hp.Random() # Random number generator object
r.Gauss(-2.0, 1.0, container)
# Above will generate 1000000 with mean -2.0 and sigma as 1.0
The Exponential random number generation method or Exp
method in Random class
generates the numbers with the given tau
value of the Exponential distribution.
import HydraPython as hp
container = hp.host_vector_float(100) # Continer to hold 100 objects.
r = hp.Random() # Random number generator object
r.Exp(1.0, container) # tau is 1.0
print (container)
The Random class also provides a BreitWigner
method to generate random number
according to a BreitWigner with mean
and width
.
import HydraPython as hp
container = hp.device_vector_float(10000) # Continer to hold 10000 objects.
r = hp.Random() # Random number generator object
r.BreitWigner(2.0, 0.2, container) # mean=2.0, width=0.2
print (container)
Apart from all these distributions, you can also define your own distribution
and pass it as a function to the method. The Sample
method allows you to pass
a function that will be sampled for the given sampling range (lower, upper) and
store the result in the container.
import HydraPython as hp
# The functon which will be sampled.
import math
def gauss1(*args):
g = 1.0
mean = -2.0
sigma = 1.0
for i in range(3):
m2 = (args[i] - mean) * (args[i] - mean)
s2 = sigma * sigma
g *= math.e ** ((-m2/(2.0 * s2 ))/( math.sqrt(2.0*s2*math.pi)))
return g
container = hp.host_vector_float3(10000) # Container with 10000 objects each having 3 floats
r = hp.Random() # Random object
r.Sample(d, [6, 6, 6], [-6, -6, -6], gauss1)
# d is container, [6, 6, 6] is the start range (1 for each float in container),
# [-6, -6, -6] is end range, gauss1 is the functor.
In sample method, the start range and end range should have the same number of
arguments as in the container. So for example, if you are using container of
float7
than start range and end range each should contain 7
elements.
Warning
Any of device containers will not work with Sample
method.
The complete method list supported by Random class can be found on [1].
The container list that can be passed to Sample
method can be found on [2].
[1] | The method list for Random classes
|
[2] | The list of available containers to use with Random.
|