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
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
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
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
:microphone: ElixirConf 2026 - Call for Talks is open!
We’re heading to Chicago :united_states:
:round_pushpin: In person + virtual
:d...
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
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 23 to 14- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
PatNowak
Ahh, ok. Inside
Enum. Thanks thenjosevalim
As said above, it is already part of the language:
Enum.random(1..3).PatNowak
This
rand(a..b)is very useful and needed. It reminds RubyUsing
:randor:randomcan be painful, because of lack of documentation.@josevalim, incorporate this
randin The Language and polish the docssztosz
It was late, that’s why I missed it, I think
But thank you very much for answer.
josevalim
The reason is in the commit message, Enum.random/1 already provides the functionality, so we decided to stick with it.
sztosz
Hey does anyone knows what happend to this? I can’t find rand on master branch elixir/lib/elixir/lib/kernel.ex at main · elixir-lang/elixir · GitHub nor 1.4 branch it seems that it was added here: Add Kernel.rand/0 and Kernel.rand/1 · elixir-lang/elixir@786da1f · GitHub and removed here: Remove Kernel.rand/0 and Kernel.rand/1 · elixir-lang/elixir@8e2a1e5 · GitHub
@josevalim: Was it meant to be reintroduced with
randomname instead ofrandand somehow did not because someone people just forget things? Or was there more to the story of removal of that code?hubertlepicki
I have a personal theory that in fact there are at least 3 @josevalim’s. You never know who is on the other end, since the visual contact is so rare. It’s physically impossible for one person to do so many awesome things.
OvermindDL1
Yeah Jose is truly awesome. It is so pleasant to be around a BDFL that speaks so nicely. ^.^
Hal9000
Hubert speaks truth
@josevalim is awesome on more than one level.
I will certainly learn this procedure for next time.
As for contributors’ list – if I ever contribute 1,000 lines of code, I will
I’m grateful to be mentioned, but I wouldn’t ask for mention
care more.
for 20 or so LOC.
Cheers,
Hal
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: Contributors to elixir-lang/elixir · GitHub). 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