andreyuhai

andreyuhai

If we always use the same seed then we always get the same number with :rand.uniform(10). I was wondering whether we can do that with random strings as well, I mean obviously we can but I was wondering whether there’s already that functionality in any library.

Why I need it is that, I need to create an API, and you don’t keep any data anywhere except encoding into the API token. Given the same API token you always have to generate the same information every time, so I thought using seeds would be a good approach.

One of them is this gist which actually uses Enum.random.

Showing Posts 1 to 5

stefanchrobot

stefanchrobot

Not responding directly to your question, but it sounds like you want to hash something rather than generate a random value. I’d suggest looking into Plug.Crypto and JWTs.

andreyuhai

andreyuhai OP

Yes, I am already using Plug.Crypto.sign/4 to generate an API token with some data. However I can not encode everything into the API token because then it will be huge and it doesn’t make sense to encode everything into the API token in my case.

So instead what I did is I am encoding a %{random_seed: 123} into the token and then using that seed I am generating numerical data for some fields and for some fields I need string data so I would also like to generate a string using the same seed so that every time I use the API token I will get the same results.

I think it’s also good to mention that random data is OK as far as it’s consistent which is what I am trying to do. Because this is going to be a sandbox API.

Maybe I’ll be using the gist I’ve shared above just to get going and I’ll figure it out later.

LostKobrakai

LostKobrakai

How do you generate the strings at the moment?

andreyuhai

andreyuhai OP

I didn’t have time to implement something to generate the strings but it seems like I will be using the gist I’ve shared above until I find something better. Someone also turned it into a package I guess:

https://github.com/jeremytregunna/randomizer

The random string will be part of an id like foo_{RAND_STR_HERE} and then later it will be required to access that specific entity like /cars/:id

03juan

03juan

Simple generation of seeded, URL-safe strings (github.com)

Here is my attempt at modifying the gist for seeded generation. I took the liberty of simplifying the code a bit into a Livebook.

I don’t have any experience with :rand so I don’t know if you need to reset the seed generation to pseudo-random after generating in randomize/3, to prevent it affecting other parts of an application.

In my opinion it seems a bit overkill to create a whole library for this, but that’s not relevant to the question.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews