Similar presentations:
Programming 1 term 1 lesson Random (1)
1. Programming Grade 12
2.
3. Python libraries.
4. Functions for working with random numbers.
• 12.6.1.1 use the functions of the library Random to get a pseudorandom number;• 12.6.1.2 determine the function of the library Random for the
specified task;
• 12.6.1.3 identify code snippets that use random sequence generation;
• 12.6.1.4 solve problems using random numbers and random
sequences;
5. Where can we use random numbers?
6. Applications of Random numbers
1. Simulations and Modeling:1. Random numbers are used in simulations to
model complex systems and processes, such as
weather forecasting, financial modeling, and
scientific research.
2. Cryptography:
1. Random numbers are essential in cryptography
for generating secure keys, initialization vectors,
and other cryptographic elements to ensure
secure communication and data protection.
3. Gaming:
6. Randomized Algorithms:
1. Some algorithms, like randomized quicksort or the
random walk algorithm, use random numbers to
improve performance or make decisions during
execution.
7. Lottery and Gambling:
1. Random numbers are crucial in lotteries, slot
machines, and other forms of gambling to ensure
fairness and unpredictability.
8. Procedural Content Generation:
1. In creative fields like video game development,
random numbers are used to generate procedural
content such as landscapes, levels, or characters.
1. Random numbers are used in video games and
online gaming to generate random events,
outcomes, and sequences, such as shuffling cards,
9. Testing and Debugging:
rolling dice, or spawning items.
1. Random numbers are used in software testing to
4. Statistical Sampling:
create random test cases or inputs, helping to
1. In statistics, random numbers are used to select
identify edge cases and ensure the robustness of
random samples from a population for surveys,
code.
experiments, and studies, ensuring that the
sample is representative of the whole population. 10.Art and Music:
1. Random numbers can be used in generative art
5. Machine Learning:
and music to create patterns, compositions, or
1. Random numbers are used in machine learning
effects that have an element of unpredictability.
algorithms, such as in the initialization of weights
in neural networks or in random sampling
techniques like bootstrapping.
7. Random module
• Python Random module is an in-built module of Python which isused to generate random numbers.
• These are pseudo-random numbers means these are not truly
random.
• This module can be used to perform random actions such as
generating random numbers, print random a value for a list or
string, etc.
8. Generate random item from a list
• choice() is an inbuilt function inPython programming language that
returns a random item from a list,
tuple, or string.
random.choice(sequence)
sequence is a mandatory parameter
that can be a list, tuple, or string.
Returns: The choice() returns a
random item.
Note: We have to import random to
use choice() method.
9. What will the program output?
import randomnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
rand = random.choice(numbers)
print(rand)
• [1, 2, 3, 4, 5, 6, 7, 8, 9]
• [9, 1, 3, 4, 5, 6, 7, 8, 2]
•8
• 10
•5
• False
• False
• True
• False
• True
10. Task 1
11. Generate random integers between the given range
• random.randint() method isused to generate random
integers between the given
range.
Syntax:
randint(start, end)
start <= Number <= end
12. What will the program output?
import randomnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
rand = random.randint(2, 7)
print(rand)
•1
•2
•3
•8
•7
• False
• True
• True
• False
• True
13. Task 2
14. Task 3
15. Creating Random Floats
random.random() method is usedto generate random floats
between 0.0 to 1.
Syntax:
• random.random()
16. Shuffle function
• Shuffles a given list.17. Task 4
18. Research task
• Give examples of using other functions of the random moduleSample (population, k), choices(), seed(), shuffle(), uniform(),
triangular(low, high, mode), getstate() and setstate() etc.
• https://www.geeksforgeeks.org/python-random-module/
19.
https://www.geeksforgeeks.org/python-random-module/20. Reflection
A. How can you demonstrate yourunderstanding?
B. How can you apply what you have learned?
21. References
• https://python-scripts.com/random?#random-element-list• https://www.geeksforgeeks.org/python-random-module/
• https://metanit.com/python/tutorial/6.1.php