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
Hey everyone!
Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application.
This library uses Erlang esaml to provide
plug enabl...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly.
One of i...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Other Trending Topics
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
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
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./
The firs...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- 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.
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).Hal9000
I’m happy to assist any way I can.
Hal
josevalim
If you want to send a proposal, the planned API for
rand, similar to what you have sent here, is enough. However, given the proposal is pretty much here already, it is ok to bypass that and send a PR if you prefer.We don’t plan to have any seed or stateful function. The goal is to use
rand/0andrand/1as a gateway to the actual:randmodule. So we can explicitly document that you can get control over the seed by using the:randmodule.Hal9000
I don’t mind sending a proposal.
I just don’t know to whom I should send it.
Hal
OvermindDL1
It would be to the central Elixir github I would imagine (correct me otherwise?): GitHub - elixir-lang/elixir: Elixir is a dynamic, functional language for building scalable and maintainable applications · GitHub
If you can get a PR (Pull Request) started with the basic code as jose wants, then conversation can happen within that PR to refine it so you can add the refinements until it gets merged into core.
Hal9000
I don’t know the source. It’s not obvious to me where this would go.
If anyone wants to assist, let me know. Or if I can simply submit
code/docs to someone, that is fine, too.
rand/0 and rand/1 are very few lines of code.
Hal
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!
Hal9000
I cloned the repo and added two functions and two tests (which pass).
Not sure how to proceed. Tried a push - got this:
ERROR: Sorry, but @elixir-lang has blocked access to SSH keys created by
some third-party applications. Your key was created before GitHub tracked
keys created by applications, so we need your help.
[blah blah, omitted]
Duh?
Hal
OvermindDL1
Look up in the github docs about ‘forking’ and ‘making a pull request’. Basically you will fork (copy) their repo into your own place. Clone that one down. Branch it with a decent name (“add_rand” for example), make your changes, push those up to your repo, then in github submit a pull request on it (big button on the interface you cannot miss when you have a special feature branch like that).
I would give exact commands, but in a hurry. It is easy though once learned and github well documents it.