Hal9000
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(althoughuniformandnormalwill still work, for people accustomed to those). randtakes a parameter:normalfor normal-distribution results.- The
_sfunctions 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
seedandseed3(the latter of which takes a tuple of three integers rather than a state). - The functions
export_seedandexport_seed_sare not really needed any longer. - The
randfunction can take a range, e.g.rand(50..60). - The
Randommodule addssample(which takes a second parameter, a number defaulting to 0); this callsEnum.take_randomwhich works on any enumerable. - The
Randommodule addsshuffle(which simply callsEnum.shuffleand thus can take any enumerable. - The PRNG is seeded on module load (default algorithm is
:exsplusjust 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.
Trending in Announcing
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
Hi everyone,
I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
I’ll shortly be launching Text, a nascent text analysis library.
Current functionality
In this early version (not ready for prime time) ...
New
Following on from my CLDR lbraries I started work on Unicode transforms. But like everything related to CLDR there is a lot of yak-shavin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











First Post!- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Looks good. Unsure when I would get a chance to try it sadly, but good interface.
Most Liked
josevalim
I really like this too! I would love to hijack the
randfunction 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.The only change I would possibly do is to drop support for
rand(integer)sincerand(0..5)is much more explicit thanrand(5).josevalim
As said above, it is already part of the language:
Enum.random(1..3).josevalim
You can clone the Elixir repo and then run:
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:
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!
Last Post!
PatNowak
Ahh, ok. Inside
Enum. Thanks then