crisefd

crisefd

Why does my random generation of strings fails?

Let me preface this by saying that I know I can fix my problem by using a library but this is just me doing some elixir exercises for fun. Ok, here is my problem, I want to model a graph and so I created a Vertex struct, this struct has an id property and I want it to be self generating random unique string.

This is my code:

defmodule Vertex do
  defstruct value: nil,
            id: Base.encode16(:crypto.strong_rand_bytes(64)),
            neighbours: MapSet.new([]),

  def new(value), do: %Vertex{value: value}

  def add_neighbour(
      ... Some code here
  end


The unique id generation code is something I copy pasted from a elixir course I took. The problem is that when I try to create vertices I get the same string every time

iex(1)> %Vertex{value: "Bryan"}
%Vertex{
  value: "Bryan",
  id: "59832511CE7A95FFA4BC22FD5A28581DD40AA20C93ACC3E6BAB9B178F6C8D11F3AF14A5EB047846384D5B5CC4B64609C4ABCFFC6D9C4260AA3551A97899253D5",
  neighbours: MapSet.new([]),
}
iex(2)> %Vertex{value: "Cris"}
%Vertex{
  value: "Cris",
  id: "59832511CE7A95FFA4BC22FD5A28581DD40AA20C93ACC3E6BAB9B178F6C8D11F3AF14A5EB047846384D5B5CC4B64609C4ABCFFC6D9C4260AA3551A97899253D5",
  neighbours: MapSet.new([]),
}

But when I execute just the function call it works:

iex(3)> Base.encode16(:crypto.strong_rand_bytes(64))
"16D7500A3A7629E7D83D65E35D3B49CF4D5EB55D3464EFBC44753F268D6C0457C4EC366C7F27CF7BC72A99F8FC7E407BAA9D28E4C39B1F97AB1083174D3C4C1A"
iex(4)> Base.encode16(:crypto.strong_rand_bytes(64))
"D950A48722542628849FD7A0097DE9E7324C4426F6697FEB1A2C35968981FE0B218148CB281BA08B4CEEAC5F3DABAC2C129CF258E466E6FCBF699B2CAA9C7076"

Why is this happening ? It seems like seeding problem but I want someone to explain it to me please.

Marked As Solved

sbuttgereit

sbuttgereit

Because the function calls here are being run at compile time, not runtime. In essence, you’re setting the default to the result of the function call (a call made once at compile time), not saying run the function to generate a default when a new instance of the struct is made at runtime.

Since you have a new/1 function anyway… you could do something like this:

def new(value) do 
  %Vertex{
    id: Base.encode16(:crypto.strong_rand_bytes(64)), 
    value: value}
end

That would be runtime generation.

Also Liked

crisefd

crisefd

Yeah, that was it. Thanks a lot

Where Next?

Popular in Questions Top

pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement