class_randomnumbergenerator.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.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. Properties
  21. ----------
  22. +-----------------------+--------------------------------------------------------+--------------------------+
  23. | :ref:`int<class_int>` | :ref:`seed<class_RandomNumberGenerator_property_seed>` | ``-6398989897141750821`` |
  24. +-----------------------+--------------------------------------------------------+--------------------------+
  25. Methods
  26. -------
  27. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`float<class_float>` | :ref:`randf<class_RandomNumberGenerator_method_randf>` **(** **)** |
  29. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :ref:`float<class_float>` | :ref:`randf_range<class_RandomNumberGenerator_method_randf_range>` **(** :ref:`float<class_float>` from, :ref:`float<class_float>` to **)** |
  31. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | :ref:`float<class_float>` | :ref:`randfn<class_RandomNumberGenerator_method_randfn>` **(** :ref:`float<class_float>` mean=0.0, :ref:`float<class_float>` deviation=1.0 **)** |
  33. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`int<class_int>` | :ref:`randi<class_RandomNumberGenerator_method_randi>` **(** **)** |
  35. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`int<class_int>` | :ref:`randi_range<class_RandomNumberGenerator_method_randi_range>` **(** :ref:`int<class_int>` from, :ref:`int<class_int>` to **)** |
  37. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | void | :ref:`randomize<class_RandomNumberGenerator_method_randomize>` **(** **)** |
  39. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  40. Property Descriptions
  41. ---------------------
  42. .. _class_RandomNumberGenerator_property_seed:
  43. - :ref:`int<class_int>` **seed**
  44. +-----------+--------------------------+
  45. | *Default* | ``-6398989897141750821`` |
  46. +-----------+--------------------------+
  47. | *Setter* | set_seed(value) |
  48. +-----------+--------------------------+
  49. | *Getter* | get_seed() |
  50. +-----------+--------------------------+
  51. The seed used by the random number generator. A given seed will give a reproducible sequence of pseudo-random numbers.
  52. **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.
  53. Method Descriptions
  54. -------------------
  55. .. _class_RandomNumberGenerator_method_randf:
  56. - :ref:`float<class_float>` **randf** **(** **)**
  57. Generates a pseudo-random float between ``0.0`` and ``1.0`` (inclusive).
  58. ----
  59. .. _class_RandomNumberGenerator_method_randf_range:
  60. - :ref:`float<class_float>` **randf_range** **(** :ref:`float<class_float>` from, :ref:`float<class_float>` to **)**
  61. Generates a pseudo-random float between ``from`` and ``to`` (inclusive).
  62. ----
  63. .. _class_RandomNumberGenerator_method_randfn:
  64. - :ref:`float<class_float>` **randfn** **(** :ref:`float<class_float>` mean=0.0, :ref:`float<class_float>` deviation=1.0 **)**
  65. 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.
  66. ----
  67. .. _class_RandomNumberGenerator_method_randi:
  68. - :ref:`int<class_int>` **randi** **(** **)**
  69. Generates a pseudo-random 32-bit unsigned integer between ``0`` and ``4294967295`` (inclusive).
  70. ----
  71. .. _class_RandomNumberGenerator_method_randi_range:
  72. - :ref:`int<class_int>` **randi_range** **(** :ref:`int<class_int>` from, :ref:`int<class_int>` to **)**
  73. Generates a pseudo-random 32-bit signed integer between ``from`` and ``to`` (inclusive).
  74. ----
  75. .. _class_RandomNumberGenerator_method_randomize:
  76. - void **randomize** **(** **)**
  77. Setups a time-based seed to generator.
  78. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  79. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  80. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`