You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/micropython/01.basics/08.reference/reference.md
+94-5Lines changed: 94 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -806,16 +806,105 @@ else:
806
806
807
807
808
808
## random()
809
-
<!-- TODO -->
809
+
810
+
`random.random()`
811
+
812
+
Produces a random number within the range provided.
813
+
814
+
**Example:**
815
+
816
+
```python
817
+
import random
818
+
819
+
random_integer = random.randint(1, 10)
820
+
print("Random integer between 1 and 10:", random_integer)
821
+
```
822
+
810
823
## randomSeed()
811
-
<!-- TODO -->
812
-
## External Interrupts
813
824
825
+
`seed_value = int(time.time())` and `random.seed()`
826
+
827
+
To generate a random seed value, we first use the `time()` module to generate a unique value, and feed it to the `random.seed()` generator. The result is that you will always get a unique random number.
828
+
829
+
**Example:**
830
+
831
+
```python
832
+
import random
833
+
import time
834
+
835
+
# Seed the random number generator with the current time
836
+
seed_value =int(time.time())
837
+
random.seed(seed_value)
838
+
839
+
# Generate random numbers using the seeded generator
840
+
random_number = random.randint(1,100)
841
+
print(random_number)
842
+
```
843
+
844
+
***Note that `time.time()` generates a new value every second. E.g. running `random.seed()` twice within a second will generate the same value. `random.seed()` should not be used repetitively.***
0 commit comments