Mary's character is Captain Picard, which means she has to keep the captain's log.
She loves the creative part of the game, but doesn't like to generate random data on the spot.
Help Mary by creating random generators for data commonly appearing in the captain's log.
The Starship Enterprise encounters many planets in its travels. Planets in the Star Trek universe are split into categories based on their properties. For example, Earth is a class M planet. All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.
Implement the random_planet_class/0 function. It should return one of the planetary classes at random.
Enterprise (registry number NCC-1701) is not the only starship flying around! When it rendezvous with another starship, Mary needs to log the registry number of that starship.
Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (inclusive).
Implement the random_ship_registry_number/0 function that returns a random starship registry number.
What's the use of a log if it doesn't include dates?
A stardate is a floating point number. The adventures of the Starship Enterprise from the first season of The Next Generation take place between the stardates 41000.0 and 42000.0. The "4" stands for the 24th century, the "1" for the first season.
Implement the function random_stardate/0 that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).
In the captain's log, stardates are usually rounded to a single decimal place.
Implement the format_stardate/1 function that will take a floating point number and return a string with the number rounded to a single decimal place.
https://exercism.org/tracks/elixir/exercises/captains-log
defmodule CaptainsLog do
@moduledoc """
practice random and erlang library usage
"""
@planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]
@typedoc """
string in @planetary_classes
"""
@type possible_planetary :: String.t()
@spec random_planet_class() :: possible_planetary()
def random_planet_class(), do: @planetary_classes |> Enum.random()
@spec random_ship_registry_number() :: String.t()
def random_ship_registry_number(), do: "NCC-#{1000..9999 |> Enum.random()}"
@spec random_stardate() :: float()
def random_stardate() do
:rand.uniform() * (42000 - 41000) + 41000
end
@spec format_stardate(float()) :: String.t()
def format_stardate(stardate), do: "~.1f" |> :io_lib.format([stardate]) |> to_string
end