2.3 KiB
2.3 KiB
Hints
General
- Read about using Erlang libraries in the official Getting Started guide, and in particular about formatting strings.
1. Generate a random planet
- Use the provided module attribute with a list of letters representing planetary classes.
- There is a built-in function for choosing an element from a list at random.
2. Generate a random starship registry number
- There is a built-in function for choosing an element from a range at random.
3. Generate a random stardate
- There is no Elixir function that would return a random float.
- There is a built-in Erlang function that returns a random float x where
0.0 <= x < 1.0. - If
xbelongs to a range0.0 <= x < 1.0, but you need a number from a different rangea <= x < b, you can shift x's range by multiplying it by the range's width (b - a) and adding the range's start (a). That is:x * (b - a) + a.
4. Format the stardate
- There is no Elixir function that would be able to, in a single step, format a float as a string with a given precision.
- There is a built-in Erlang function that takes a format string and a list of data, and returns a charlist.
- There is a built-in function that changes a charlist to a string.
- The format string of that function contains control sequences.
- A control sequence starts with
~and has the pattern~F.P.PadModC, whereFstands for the width of the output,Pstands for the precision,Padstands for the padding character,Modstands for the control sequence modifier, andCis the type of the control sequence. - To format a float with a desired precision, a control sequence with the pattern
~.PCwill suffice. - The control sequence type for floats is
f. - The exact format string you need is
~.1f.