Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilities for non-deterministic data generation #433

Open
tmbb opened this issue Nov 14, 2021 · 0 comments
Open

Utilities for non-deterministic data generation #433

tmbb opened this issue Nov 14, 2021 · 0 comments

Comments

@tmbb
Copy link

tmbb commented Nov 14, 2021

I've written the following very simple utility macro to generate proabilistic fake data for a project and thought other people might find it interesting. It doesn't make sense to package such a simple macro but it might make sense to include it in a larger package such as Faker. The implementation speaks for itself:

defmodule ProbabilityDemo do
  defmacro with_probability(p, [do: do_body, else: else_body]) do
    quote do
      if :rand.uniform() < unquote(p) do
        unquote(do_body)
      else
        unquote(else_body)
      end
    end
  end
end

You use it like this

iex(2)> import ProbabilityDemo
ProbabilityDemo
iex(3)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(4)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(5)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(6)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(7)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(8)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(9)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(10)> with_probability 0.3 do "Success!" else "Failure!" end
"Success!"
iex(11)> with_probability 0.3 do "Success!" else "Failure!" end
"Success!"

It saves you from writing something like the following and makes the intent clearer

# Is the first outcome success or failure? should it be < or >?
if :rand.uniform() < prob do
 # something in case of success
else
  # something in case of failure
end

In practice I'm using it for things like:

gender = with_probability 0.35 do :female else :male end

What do you think about including something like this in Faker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant