Hal9000

Hal9000

Random - based on :rand (not :random)

Here is my first stab at this. README pasted below.

https://github.com/Hal9000/elixir_random

Comments and critiques are welcome.

Thanks,
Hal


The Random module is based on Erlang’s rand module. (See rand — OTP 29.0.2 (stdlib 8.0.1) ).
This in turn is a newer replacement for Erlang’s deprecated random.

All functionality of rand is preserved here. The primary differences are:

  • The preferred function name is rand (although uniform and normal will still work, for people accustomed to those).
  • rand takes a parameter :normal for normal-distribution results.
  • The _s functions are unnecessary, as state is passed (as needed) by means of an optional parameter.
  • Any function that accepts a state also returns the new state (second item in tuple); other functions simply return a number.
  • A distinction is made between seed and seed3 (the latter of which takes a tuple of three integers rather than a state).
  • The functions export_seed and export_seed_s are not really needed any longer.
  • The rand function can take a range, e.g. rand(50..60).
  • The Random module adds sample (which takes a second parameter, a number defaulting to 0); this calls Enum.take_random which works on any enumerable.
  • The Random module adds shuffle (which simply calls Enum.shuffle and thus can take any enumerable.
  • The PRNG is seeded on module load (default algorithm is :exsplus just as with :rand); you may of course reseed at any time.

Examples

x = Random.rand             # Random number between 0.0 and 1.0

x = Random.rand(:normal)    # Random number in normally-distributed range, -1..1 is one sigma

n = Random.rand(5)          # Random integer between 1 and 5

n = Random.rand(10..20)     # Random integer between 10 and 20

state = Random.seed(:exsplus)               # Use the explus algorithm (default), Xorshift116+, 58 bits, period = 2^116-1

state = Random.seed(:exs64)                 # Use the exs64 algorithm, Xorshift64, 64 bits, period = 2^64-1

state = Random.seed(:exs1024)               # Use the exs1024 algorithm, Xorshift1024, 64 bits, period = 2^1024-1

state = Random.seed3(:exsplus, {1, 2, 3})   # Seed using the three integers 1, 2, 3

state = Random.seed(:exsplus, state)        # Seed using the specified state

x = Random.uniform                          # Same as Random.rand

x = Random.normal                           # Same as Random.rand(:normal)

{x, state} = Random.rand(state)             # Specify state and return number and a new state

{x, state} = Random.rand(:normal, state)    # Specify state and return number (normal distribution) and new state

item = Random.sample(enum)                  # Grab one random item from any enumerable (such as a list)

list = Random.sample(enum, 3)               # Grab 3 random items from any enumerable (such as a list)

list = Random.shuffle(enum)                 # Randomize (shuffle) an enumerable (such as a list)

Notes

The sample and shuffle functions do not have state-sensitive variants.

Most Liked

josevalim

josevalim

Creator of Elixir

I really like this too! I would love to hijack the rand function and make it part of Elixir’s Kernel. Would you be interested in sending a proposal or a PR to elixir for the rand/0 and rand/1 functions? It sounds like a perfect gateway to link developers to Erlang’s :rand. :slight_smile:

The only change I would possibly do is to drop support for rand(integer) since rand(0..5) is much more explicit than rand(5).

josevalim

josevalim

Creator of Elixir

As said above, it is already part of the language: Enum.random(1..3). :slight_smile:

josevalim

josevalim

Creator of Elixir

You can clone the Elixir repo and then run:

make compile

Then you can add the code implementation around here:

https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel.ex#L1537

And the tests around here:

https://github.com/elixir-lang/elixir/blob/master/lib/elixir/test/elixir/kernel_test.exs#L87

After writing code or tests, you can run the line below inside the Elixir repo to run one particular test:

elixirc lib/elixir/lib/kernel.ex -o lib/elixir/ebin && elixir lib/elixir/test/elixir/kernel_test.exs

If the tests pass, we are golden! If you would prefer to send the code and tests and not a PR, please just open up an issue!

Where Next?

Popular in Announcing Top

mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 10167 134
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have ...
New
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
zachdaniel
Ash Framework What is Ash? Ash Framework is a declarative, resource-oriented application development framework for Elixir. A resource can...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10858 107
New
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement