RosettaCodeData/Task/Constrained-random-points-on-a-circle/Elixir/constrained-random-points-on-a-circle-1.elixir

24 lines
627 B
Text
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
defmodule Random do
defp generate_point(0, _, _, set), do: set
2016-12-05 22:15:40 +01:00
defp generate_point(n, f, condition, set) do
2015-11-18 06:14:39 +00:00
point = {x,y} = {f.(), f.()}
2016-12-05 22:15:40 +01:00
if x*x + y*y in condition and not point in set,
do: generate_point(n-1, f, condition, MapSet.put(set, point)),
else: generate_point(n, f, condition, set)
2015-11-18 06:14:39 +00:00
end
2016-12-05 22:15:40 +01:00
2015-11-18 06:14:39 +00:00
def circle do
f = fn -> :rand.uniform(31) - 16 end
2016-12-05 22:15:40 +01:00
points = generate_point(100, f, 10*10..15*15, MapSet.new)
range = -15..15
for x <- range do
for y <- range do
IO.write if {x,y} in points, do: "x", else: " "
2015-11-18 06:14:39 +00:00
end
IO.puts ""
end
end
end
Random.circle