Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jun 28, 2021
1 parent d50361c commit 75f7c91
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crystal/examples/random/class.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rnd = Random.new
puts rnd.next_float
puts rnd.next_int
puts rnd.next_bool
puts rnd.hex(3) # 6 characters from hex values
puts rnd.base64(2)
5 changes: 5 additions & 0 deletions crystal/examples/random/class.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0.17048389661361377
-258843089
false
ba753d
Fd4=
2 changes: 2 additions & 0 deletions crystal/examples/random/rand.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0.9567402323460531
0
4 changes: 4 additions & 0 deletions crystal/examples/random/seed.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rnd = Random.new(23)
puts rnd.next_float
puts rnd.next_float
puts rnd.next_float
3 changes: 3 additions & 0 deletions crystal/examples/random/seed.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.6834386634915893
0.8454063470402874
0.4425350078089257
26 changes: 24 additions & 2 deletions crystal/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@
* Pseudo Random floating point number between 0 and 1

![](examples/random/rand.cr)
![](examples/random/rand.out)

## Random in other parts of Crystal
{id: other-random}

* [shuffle](arrays-shuffle)
* sample
* [shuffle array](arrays-shuffle)
* [sample array](arrays-sample)


## Random as a class
{id: random-class}
{i: next_float}
{i: next_int}
{i: next_bool}
{i: hex}
{i: base64}

![](examples/random/class.cr)
![](examples/random/class.out)

## Random - seed
{id: random-seed}
{i: seed}

* Fixed random-looking numbers.

![](examples/random/seed.cr)
![](examples/random/seed.out)

0 comments on commit 75f7c91

Please sign in to comment.