class_randomnumbergenerator.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. **Category:** Core
  10. Brief Description
  11. -----------------
  12. A class for generating pseudo-random numbers.
  13. Properties
  14. ----------
  15. +-----------------------+--------------------------------------------------------+----------------------+
  16. | :ref:`int<class_int>` | :ref:`seed<class_RandomNumberGenerator_property_seed>` | -6398989897141750821 |
  17. +-----------------------+--------------------------------------------------------+----------------------+
  18. Methods
  19. -------
  20. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`float<class_float>` | :ref:`randf<class_RandomNumberGenerator_method_randf>` **(** **)** |
  22. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | :ref:`float<class_float>` | :ref:`randf_range<class_RandomNumberGenerator_method_randf_range>` **(** :ref:`float<class_float>` from, :ref:`float<class_float>` to **)** |
  24. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`float<class_float>` | :ref:`randfn<class_RandomNumberGenerator_method_randfn>` **(** :ref:`float<class_float>` mean=0.0, :ref:`float<class_float>` deviation=1.0 **)** |
  26. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | :ref:`int<class_int>` | :ref:`randi<class_RandomNumberGenerator_method_randi>` **(** **)** |
  28. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`int<class_int>` | :ref:`randi_range<class_RandomNumberGenerator_method_randi_range>` **(** :ref:`int<class_int>` from, :ref:`int<class_int>` to **)** |
  30. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`randomize<class_RandomNumberGenerator_method_randomize>` **(** **)** |
  32. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  33. Description
  34. -----------
  35. RandomNumberGenerator is a class for generating pseudo-random numbers. It currently uses `PCG32 <http://www.pcg-random.org/>`_.
  36. **Note:** The underlying algorithm is an implementation detail. As a result, it should not be depended upon for reproducible random streams across Godot versions.
  37. To generate a random float number (within a given range) based on a time-dependant seed:
  38. ::
  39. var rng = RandomNumberGenerator.new()
  40. func _ready():
  41. rng.randomize()
  42. var my_random_number = rng.randf_range(-10.0, 10.0)
  43. Property Descriptions
  44. ---------------------
  45. .. _class_RandomNumberGenerator_property_seed:
  46. - :ref:`int<class_int>` **seed**
  47. +-----------+----------------------+
  48. | *Default* | -6398989897141750821 |
  49. +-----------+----------------------+
  50. | *Setter* | set_seed(value) |
  51. +-----------+----------------------+
  52. | *Getter* | get_seed() |
  53. +-----------+----------------------+
  54. The seed used by the random number generator. A given seed will give a reproducible sequence of pseudo-random numbers.
  55. **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.
  56. Method Descriptions
  57. -------------------
  58. .. _class_RandomNumberGenerator_method_randf:
  59. - :ref:`float<class_float>` **randf** **(** **)**
  60. Generates a pseudo-random float between ``0.0`` and ``1.0`` (inclusive).
  61. .. _class_RandomNumberGenerator_method_randf_range:
  62. - :ref:`float<class_float>` **randf_range** **(** :ref:`float<class_float>` from, :ref:`float<class_float>` to **)**
  63. Generates a pseudo-random float between ``from`` and ``to`` (inclusive).
  64. .. _class_RandomNumberGenerator_method_randfn:
  65. - :ref:`float<class_float>` **randfn** **(** :ref:`float<class_float>` mean=0.0, :ref:`float<class_float>` deviation=1.0 **)**
  66. 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.
  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. .. _class_RandomNumberGenerator_method_randi_range:
  71. - :ref:`int<class_int>` **randi_range** **(** :ref:`int<class_int>` from, :ref:`int<class_int>` to **)**
  72. Generates a pseudo-random 32-bit signed integer between ``from`` and ``to`` (inclusive).
  73. .. _class_RandomNumberGenerator_method_randomize:
  74. - void **randomize** **(** **)**
  75. Setups a time-based seed to generator.