A random number generator using the PCG algorithm.
Documentation: https://hexdocs.pm/rand_pcg/
- Add
rand_pcg
to your list of dependencies inmix.exs
:
```elixir
def deps do
[{:rand_pcg, "~> 0.1.2"}]
end
```
- If you are using the GenServer option, ensure
rand_pcg
is started before your application:
```elixir
def application do
[applications: [:rand_pcg]]
end
```
# Random 32 bit integer
RandPCG.random()
# Random 32 bit based float
RandPCG.random(:float)
# n Random 32 bit integers
RandPCG.random(n)
# Random nth of an enumerable
list = [:a, :b, :c]
RandPCG.random(list)
# Random integer x where min <= x <= max
RandPCG.random(min, max)
# n random integers x where min <= x <= max
RandPCG.random(min, max, n)
state = %RandPCG.State{seed: 234532454323451, inc: 1}
RandPCG.state(state)
The initial seed is based on :os.system_time(:micro_seconds)
You will have to maintain your own state of the random number generator.
state = RandPCG.PCG.gen_state()
random_int_32 = RandPCG.PCG.xsh_rr(state)
state = advance(state)
If you do not advance the state, you will receive the same random number.
state = RandPCG.PCG.gen_state()
min = 1
max = 10
random_1_10_inclusive = RandPCG.PCG.rand_int(min, max, state)
state = advance(state)
RandPCG.PCG.gen_state
initial seed is based on :os.system_time(:micro_seconds)