Skip to content

Commit

Permalink
0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wilwade committed Jul 12, 2017
0 parents commit 2c8c2a0
Show file tree
Hide file tree
Showing 13 changed files with 10,637 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# The directory Mix will write compiled artifacts to.
/_build

# If you run "mix test --cover", coverage assets end up here.
/cover

# The directory Mix downloads your dependencies sources to.
/deps

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2017 Wil Wade.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# RandPCG

A random number generator using the [PCG](http://www.pcg-random.org/) algorithm.

Documentation: https://hexdocs.pm/rand_pcg/

## Installation

1. Add `rand_pcg` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:rand_pcg, "~> 0.1.1"}]
end
```

2. If you are using the GenServer option, ensure `rand_pcg` is started before
your application:

```elixir
def application do
[applications: [:rand_pcg]]
end
```

## Examples

### GenServer Option

Ensure the service is running if not included as an application:
`RandPCG.start_link`

#### Get Some Random

```elixir
# 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)
```

#### Set State

```elixir
state = %RandPCG.State{seed: 234532454323451, inc: 1}
RandPCG.state(state)
```

#### Note

The initial seed is based on `:os.system_time(:micro_seconds)`

### Without running the GenServer

You will have to maintain your own state of the random number generator.

#### Random 32 bit Integer

```elixir
state = RandPCG.gen_state()
random_int_32 = RandPCG.xsh_rr(state)
state = advance(state)
```

If you do not advance the state, you will receive the same random number.

#### Random Integer in a Range

```elixir
state = RandPCG.gen_state()
min = 1
max = 10
random_1_10_inclusive = RandPCG.rand_int(min, max, state)
state = advance(state)
```

`RandPCG.gen_state` initial seed is based on `:os.system_time(:micro_seconds)`

## References

- [PCG Random Number Generator Homepage](http://www.pcg-random.org/)
- [PCG Basic C Implementation](https://github.com/imneme/pcg-c-basic)
- [PCG Go Implementation](https://github.com/dgryski/go-pcgr) was also helpful
30 changes: 30 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure for your application as:
#
# config :rand_pcg, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:rand_pcg, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
14 changes: 14 additions & 0 deletions lib/helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule RandPCG.Helpers do
@moduledoc """
Bitwise helpers
"""
use Bitwise, only_operators: true

@mask32 0xFFFFFFFF

@spec rotate_right_32(RandPCG.uint64, RandPCG.uint64) :: RandPCG.uint32
def rotate_right_32(v, rotate) do
rotate = rotate &&& 31
((v >>> rotate) ||| (v <<< (-rotate &&& 31))) &&& @mask32
end
end
Loading

0 comments on commit 2c8c2a0

Please sign in to comment.