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 http://erlang.org/doc/man/rand.html ).
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:

And the tests around here:

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!

josevalim

josevalim

Creator of Elixir

No worries, I have incorporated your changes to master here: https://github.com/elixir-lang/elixir/commit/786da1ffe075d50a26af885f4baf9589978c45c2

hubertlepicki

hubertlepicki

Is it boring for me to repeat that you are increadibly good in making people contribute to Elixir? I mean, you don’t even have to know pull request flow to do so, awesome!

@Hal9000 for the future you may want to learn the procedure however. It’s both easier for the maintainer and you get some respect badge in the hood by being listed on official contributor’s list (as in here: https://github.com/elixir-lang/elixir/graphs/contributors). If maintainer just copies the code, you are not listed (although in this particular case @josevalim was kind enough to mention you in the commit description!). Some people may care about this detail, others not, just letting you know :slight_smile:

Where Next?

Popular in Libraries Top

RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 11851 134
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
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
zoltanszogyenyi
Hey everyone :wave: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-source U...
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamP...
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
mcrumm
If you would like to migrate away from node/npm/webpack while still using sass, the dart_sass package provides a installer and runner for...
New
ericlathrop
I built a silly site for Halloween that uses Phoenix Channels on the backend, and React on the frontend. I had many problems integrating ...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New

Other popular topics Top

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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Sub Categories:

We're in Beta

About us Mission Statement