Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cleans up code with feeding pets and adds batch coin flips random check #1717

Merged
merged 1 commit into from
Apr 4, 2024

Conversation

kamronbatman
Copy link
Contributor

@kamronbatman kamronbatman commented Apr 4, 2024

Summary

  • Removes unnecessary extra loops in increasing loyalty while feeding pets.
  • Adds Utility.CoinFlips for batch 50/50 successive calls to RNG
  • Removes improper usage of Utility.RandomSample() and instead uses Shuffle()

Tip

DEVELOPER TIP
Use Utility.CoinFlips(amount) or Utility.CoinFlips(amount, max) instead of looping and doing Utility.RandomBool() multiple times. The new method is up to 60x faster.

Note

DEVELOPER NOTE
Utility.RandomSample is for sampling unique indices and has no guard rails against duplicate values

Benchmarks

Benchmarks between a loop calling RandomBool() and the new CoinFlips method.

| Method     | Amount | Mean          | Error       | StdDev      | Median        | Allocated |
|----------- |------- |--------------:|------------:|------------:|--------------:|----------:|
| LoopFlip   | 1      |      5.394 ns |   0.0530 ns |   0.0470 ns |      5.394 ns |         - |
| LoopBitPop | 1      |      1.300 ns |   0.0218 ns |   0.0193 ns |      1.300 ns |         - |
| LoopFlip   | 10     |     54.790 ns |   0.6207 ns |   0.5502 ns |     54.924 ns |         - |
| LoopBitPop | 10     |      1.316 ns |   0.0150 ns |   0.0133 ns |      1.316 ns |         - |
| LoopFlip   | 50     |    253.087 ns |   5.0548 ns |   5.8212 ns |    253.324 ns |         - |
| LoopBitPop | 50     |      1.335 ns |   0.0498 ns |   0.0911 ns |      1.292 ns |         - |
| LoopFlip   | 200    |    964.136 ns |  18.6429 ns |  20.7215 ns |    962.822 ns |         - |
| LoopBitPop | 200    |      5.630 ns |   0.1342 ns |   0.2009 ns |      5.588 ns |         - |
| LoopFlip   | 1000   |  4,926.865 ns |  97.2654 ns |  95.5276 ns |  4,916.325 ns |         - |
| LoopBitPop | 1000   |     22.335 ns |   0.4517 ns |   0.5713 ns |     22.103 ns |         - |
| LoopFlip   | 10000  | 48,465.194 ns | 617.2915 ns | 577.4149 ns | 48,453.192 ns |         - |
| LoopBitPop | 10000  |    202.858 ns |   3.4271 ns |   3.2057 ns |    203.523 ns |         - |

@kamronbatman kamronbatman changed the title fix: Fixes minor bug with Loyalty and randomness fix: Fixes minor bug with Loyalty and adds CoinFlips Apr 4, 2024
@kamronbatman kamronbatman changed the title fix: Fixes minor bug with Loyalty and adds CoinFlips fix: Cleans up code with feeding pets and adds CoinFlips Apr 4, 2024
else if (m_Loyalty < MaxLoyalty)
{
// 50% chance to increase 10 loyalty per food
m_Loyalty = Math.Min(MaxLoyalty, Utility.CoinFlips(amount, MaxLoyaltyIncrease) * 10);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bulk of the change here.

@kamronbatman kamronbatman changed the title fix: Cleans up code with feeding pets and adds CoinFlips fix: Cleans up code with feeding pets and adds batch coin flips random check Apr 4, 2024
@kamronbatman kamronbatman merged commit 29ea813 into main Apr 4, 2024
13 checks passed
@kamronbatman kamronbatman deleted the kbatman/cleanup_loyalty branch April 4, 2024 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant