WorksheetsJ277 - 2.2 - Random Number Generation
Total questions: 8
Worksheet time: 4mins
What is the purpose of random number generation in programming?
To create predictable sequences of numbers
To simulate randomness in applications
To sort numbers in ascending order
To convert numbers to strings
Which Python module is commonly used for generating random numbers?
math
random
statistics
numpy
How do you generate a random integer between 1 and 10 using the random module in Python?
`random.random(1, 10)`
`random.randint(1, 10)`
`random.range(1, 10)`
`random.int(1, 10)`
Which function from the random module would you use to select a random element from a list?
`random.choice()`
`random.select()`
`random.pick()`
`random.element()`
If you have a list `fruits = ['apple', 'banana', 'cherry', 'date']`, which of the following code snippets will randomly select a fruit from the list?
`random.choice(fruits)`
`random.randint(fruits)`
`random.random(fruits)`
`random.select(fruits)`
Which of the following functions from the random module can be used to shuffle a list in place?
`random.shuffle()`
`random.mix()`
`random.scramble()`
`random.reorder()`
How can you generate a random floating-point number between 0 and 1 using the random module?
`random.uniform(0, 1)`
`random.random()`
`random.float()`
`random.rand()`
Which function from the random module would you use to generate a random sample of 3 elements from a list `numbers = [1, 2, 3, 4, 5, 6]`?
`random.choice(numbers, 3)`
`random.sample(numbers, 3)`
`random.select(numbers, 3)`
`random.pick(numbers, 3)`
