class_randomnumbergenerator.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the RandomNumberGenerator.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_RandomNumberGenerator:
  6. RandomNumberGenerator
  7. =====================
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. A class for generating pseudo-random numbers.
  10. Description
  11. -----------
  12. RandomNumberGenerator is a class for generating pseudo-random numbers. It currently uses `PCG32 <http://www.pcg-random.org/>`__.
  13. **Note:** The underlying algorithm is an implementation detail. As a result, it should not be depended upon for reproducible random streams across Godot versions.
  14. To generate a random float number (within a given range) based on a time-dependant seed:
  15. ::
  16. var rng = RandomNumberGenerator.new()
  17. func _ready():
  18. rng.randomize()
  19. var my_random_number = rng.randf_range(-10.0, 10.0)
  20. **Note:** The default values of :ref:`seed<class_RandomNumberGenerator_property_seed>` and :ref:`state<class_RandomNumberGenerator_property_state>` properties are pseudo-random, and changes when calling :ref:`randomize<class_RandomNumberGenerator_method_randomize>`. The ``0`` value documented here is a placeholder, and not the actual default seed.
  21. Tutorials
  22. ---------
  23. - :doc:`../tutorials/math/random_number_generation`
  24. Properties
  25. ----------
  26. +-----------------------+----------------------------------------------------------+-------+
  27. | :ref:`int<class_int>` | :ref:`seed<class_RandomNumberGenerator_property_seed>` | ``0`` |
  28. +-----------------------+----------------------------------------------------------+-------+
  29. | :ref:`int<class_int>` | :ref:`state<class_RandomNumberGenerator_property_state>` | ``0`` |
  30. +-----------------------+----------------------------------------------------------+-------+
  31. Methods
  32. -------
  33. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`float<class_float>` | :ref:`randf<class_RandomNumberGenerator_method_randf>` **(** **)** |
  35. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`float<class_float>` | :ref:`randf_range<class_RandomNumberGenerator_method_randf_range>` **(** :ref:`float<class_float>` from, :ref:`float<class_float>` to **)** |
  37. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`float<class_float>` | :ref:`randfn<class_RandomNumberGenerator_method_randfn>` **(** :ref:`float<class_float>` mean=0.0, :ref:`float<class_float>` deviation=1.0 **)** |
  39. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`int<class_int>` | :ref:`randi<class_RandomNumberGenerator_method_randi>` **(** **)** |
  41. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`int<class_int>` | :ref:`randi_range<class_RandomNumberGenerator_method_randi_range>` **(** :ref:`int<class_int>` from, :ref:`int<class_int>` to **)** |
  43. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | void | :ref:`randomize<class_RandomNumberGenerator_method_randomize>` **(** **)** |
  45. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  46. Property Descriptions
  47. ---------------------
  48. .. _class_RandomNumberGenerator_property_seed:
  49. - :ref:`int<class_int>` **seed**
  50. +-----------+-----------------+
  51. | *Default* | ``0`` |
  52. +-----------+-----------------+
  53. | *Setter* | set_seed(value) |
  54. +-----------+-----------------+
  55. | *Getter* | get_seed() |
  56. +-----------+-----------------+
  57. Initializes the random number generator state based on the given seed value. A given seed will give a reproducible sequence of pseudo-random numbers.
  58. **Note:** The RNG does not have an avalanche effect, and can output similar random streams given similar seeds. Consider using a hash function to improve your seed quality if they're sourced externally.
  59. **Note:** Setting this property produces a side effect of changing the internal :ref:`state<class_RandomNumberGenerator_property_state>`, so make sure to initialize the seed *before* modifying the :ref:`state<class_RandomNumberGenerator_property_state>`:
  60. ::
  61. var rng = RandomNumberGenerator.new()
  62. rng.seed = hash("Godot")
  63. rng.state = 100 # Restore to some previously saved state.
  64. **Warning:** the getter of this property returns the previous :ref:`state<class_RandomNumberGenerator_property_state>`, and not the initial seed value, which is going to be fixed in Godot 4.0.
  65. ----
  66. .. _class_RandomNumberGenerator_property_state:
  67. - :ref:`int<class_int>` **state**
  68. +-----------+------------------+
  69. | *Default* | ``0`` |
  70. +-----------+------------------+
  71. | *Setter* | set_state(value) |
  72. +-----------+------------------+
  73. | *Getter* | get_state() |
  74. +-----------+------------------+
  75. The current state of the random number generator. Save and restore this property to restore the generator to a previous state:
  76. ::
  77. var rng = RandomNumberGenerator.new()
  78. print(rng.randf())
  79. var saved_state = rng.state # Store current state.
  80. print(rng.randf()) # Advance internal state.
  81. rng.state = saved_state # Restore the state.
  82. print(rng.randf()) # Prints the same value as in previous.
  83. **Note:** Do not set state to arbitrary values, since the random number generator requires the state to have certain qualities to behave properly. It should only be set to values that came from the state property itself. To initialize the random number generator with arbitrary input, use :ref:`seed<class_RandomNumberGenerator_property_seed>` instead.
  84. Method Descriptions
  85. -------------------
  86. .. _class_RandomNumberGenerator_method_randf:
  87. - :ref:`float<class_float>` **randf** **(** **)**
  88. Generates a pseudo-random float between ``0.0`` and ``1.0`` (inclusive).
  89. ----
  90. .. _class_RandomNumberGenerator_method_randf_range:
  91. - :ref:`float<class_float>` **randf_range** **(** :ref:`float<class_float>` from, :ref:`float<class_float>` to **)**
  92. Generates a pseudo-random float between ``from`` and ``to`` (inclusive).
  93. ----
  94. .. _class_RandomNumberGenerator_method_randfn:
  95. - :ref:`float<class_float>` **randfn** **(** :ref:`float<class_float>` mean=0.0, :ref:`float<class_float>` deviation=1.0 **)**
  96. Generates a `normally-distributed <https://en.wikipedia.org/wiki/Normal_distribution>`__ pseudo-random number, using Box-Muller transform with the specified ``mean`` and a standard ``deviation``. This is also called Gaussian distribution.
  97. ----
  98. .. _class_RandomNumberGenerator_method_randi:
  99. - :ref:`int<class_int>` **randi** **(** **)**
  100. Generates a pseudo-random 32-bit unsigned integer between ``0`` and ``4294967295`` (inclusive).
  101. ----
  102. .. _class_RandomNumberGenerator_method_randi_range:
  103. - :ref:`int<class_int>` **randi_range** **(** :ref:`int<class_int>` from, :ref:`int<class_int>` to **)**
  104. Generates a pseudo-random 32-bit signed integer between ``from`` and ``to`` (inclusive).
  105. ----
  106. .. _class_RandomNumberGenerator_method_randomize:
  107. - void **randomize** **(** **)**
  108. Setups a time-based seed to generator.
  109. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  110. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  111. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`