|
| 1 | +# 12. rand vs Raylib.get_random_value |
| 2 | + |
| 3 | +Date: 2023-10-25 |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +Accepted |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +In the context of our project, we need to decide whether to use Ruby's `rand` function or Raylib's C |
| 12 | +counterpart (`getRandomValue()`/`Raylib.get_random_value`) to generate random numbers. |
| 13 | + |
| 14 | +Here's the C implementation of `getRandomValue()` from raylib: |
| 15 | +```c |
| 16 | +// Get a random value between min and max (both included) |
| 17 | +// WARNING: Ranges higher than RAND_MAX will return invalid results |
| 18 | +// More specifically, if (max - min) > INT_MAX there will be an overflow, |
| 19 | +// and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold |
| 20 | +int GetRandomValue(int min, int max) |
| 21 | +{ |
| 22 | + if (min > max) |
| 23 | + { |
| 24 | + int tmp = max; |
| 25 | + max = min; |
| 26 | + min = tmp; |
| 27 | + } |
| 28 | + |
| 29 | + if ((unsigned int)(max - min) > (unsigned int)RAND_MAX) |
| 30 | + { |
| 31 | + TRACELOG(LOG_WARNING, "Invalid GetRandomValue() arguments, range should not be higher than %i", RAND_MAX); |
| 32 | + } |
| 33 | + |
| 34 | + return (rand()%(abs(max - min) + 1) + min); |
| 35 | +} |
| 36 | +``` |
| 37 | +
|
| 38 | +__Consistency and Maintainability__: Our project primarily uses Ruby as the programming language. By using Ruby's |
| 39 | +built-in rand function, we maintain code consistency, making it easier for developers to understand and maintain the |
| 40 | +codebase. Using C's rand() function would introduce a different paradigm and potentially increase cognitive load for |
| 41 | +the development team. |
| 42 | +
|
| 43 | +__Platform Independence__: Ruby's rand function is part of the Ruby standard library and is available across different |
| 44 | +platforms and Ruby implementations. This ensures that our random number generation code will work consistently |
| 45 | +regardless of the environment. |
| 46 | +
|
| 47 | +__Ruby Community and Ecosystem__: Leveraging Ruby's rand function allows us to take advantage of the extensive Ruby |
| 48 | +community and ecosystem. There are a wealth of libraries and resources available for working with random numbers in |
| 49 | +Ruby, which can save development time and effort. |
| 50 | +
|
| 51 | +__Ease of Use__: Ruby's rand function is straightforward to use and provides a clean and intuitive API for generating |
| 52 | +random numbers within specified ranges. This ease of use aligns with Ruby's philosophy of developer friendliness. |
| 53 | +
|
| 54 | +## Decision |
| 55 | +
|
| 56 | +Use Ruby's `rand` function for generating random numbers in our project. Here is an example of using Ruby's `rand` |
| 57 | +function to generate a random number between 1 and 10: |
| 58 | +
|
| 59 | +```ruby |
| 60 | +random_number = rand(1..10) |
| 61 | +``` |
| 62 | +This code is concise, readable, and integrates seamlessly with the Ruby codebase. |
| 63 | + |
| 64 | +## Consequences |
| 65 | + |
| 66 | +### Negative |
| 67 | + |
| 68 | +- The specific behavior of the rand function may vary slightly between Ruby versions and implementations. Developers |
| 69 | + should be aware of these variations when working with random numbers in Ruby. |
| 70 | + |
| 71 | +### Positive |
| 72 | + |
| 73 | +- Ensure code consistency, maintainability, and platform independence. |
| 74 | +- Align with the conventions and philosophy of the Ruby language. |
0 commit comments