Skip to content

Commit

Permalink
copy
Browse files Browse the repository at this point in the history
  • Loading branch information
zats committed Aug 16, 2015
1 parent 40dea16 commit 1369d87
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions _posts/2015-08-15-randoms.md
Expand Up @@ -7,12 +7,13 @@ published: True

---

GameplayKit brings random numbers generation among other useful things. So what's the difference between `GKRandomDistribution` and `GKShuffledDistribution`? As the name suggests, latter is useful for when you want to simulate card deck behaviour. Pulling a random card one after another. Until you go through entire deck, there is no way you will see the same card twice (putting magic tricks aside).
This exact behaviour is modelled by `GKShuffledDistribution`. To illustrate it, here is a bit of code:
GameplayKit brings random numbers generation among other useful things. So what's the difference between `GKRandomDistribution`, `GKGaussianDistribution` and `GKShuffledDistribution`?
As the name suggests, `GKShuffledDistribution` is useful for when you want to simulate card deck behaviour. Pulling a random card one after another. Until you go through entire deck, there is no way you will see the same card twice (putting magic tricks aside).
`GKGaussianDistribution` is targeting problem of generating random numbers around the center of the range (2 in the example below). Here is a little sample, illustrating the differences:

{% gist zats/b8d9da7effd89b0e5400 %}

Here is the output:
Here is the output it produces:

| Value | Random | Shuffled | Gausian |
| :-- | :--: | :--: | :--: |
Expand All @@ -24,4 +25,6 @@ And a visual version:

![](/assets/2015-08-15/random.png)

Although `GKRandomDistribution` instance produces equally distributed random numbers, it can result in so called "lucky strikes" - getting same value several times in a row. That's exactly when you want to use `GKShuffledDistribution`. As for gausian distribution, it is targeting case when you need to generate random numbers around center, not getting too close to the upper and lower bounds. Very welcomed additions to Cocoa family.
Although `GKRandomDistribution` instance produces equally distributed random numbers, it can result in so called "lucky streaks" - getting same value several times in a row. That's exactly when you want to use `GKShuffledDistribution`. As for `GKGaussianDistribution`, it targets cases when you need to generate random numbers around center, not getting too close to the upper and lower bounds (1 and 3 in our example).

Very welcomed additions to Cocoa family.

0 comments on commit 1369d87

Please sign in to comment.