Computer Science
Consider the following code :
import random
print(int(20 + random.random() * 5), end = ' ')
print(int(20 + random.random() * 5), end = ' ')
print(int(20 + random.random() * 5), end = ' ')
print(int(20 + random.random() * 5))
Find the suggested output options 1 to 4. Also, write the least value and highest value that can be generated.
- 20 22 24 25
- 22 23 24 25
- 23 24 23 24
- 21 21 21 21
Answer
23 24 23 24
21 21 21 21
The lowest value that can be generated is 20 and the highest value that can be generated is 24.
Explanation
import random
— This line imports the random module.print(int(20 + random.random() * 5), end=' ')
— This line generates a random float number usingrandom.random()
, which returns a random float in the range (0.0, 1.0) exclusive of 1. The random number is then multiplied with 5 and added to 20. The int() function converts the result to an integer by truncating its decimal part. Thus,int(20 + random.random() * 5)
will generate an integer in the range [20, 21, 22, 23, 24]. The end=' ' argument in the print() function ensures that the output is separated by a space instead of a newline.- The next print statements are similar to the second line, generating and printing two more random integers within the same range.
The lowest value that can be generated is 20 because random.random() can generate a value of 0, and (0 * 5) + 20 = 20. The highest value that can be generated is 24 because the maximum value random.random() can return is just less than 1, and (0.999… * 5) + 20 = 24.999…, which is truncated to 24 when converted to an integer.
Related Questions
Suppose that after we import the random module, we define the following function called
diff
in a Python session :def diff(): x = random.random() - random.random() return(x)
What would be the result if you now evaluate
y = diff() print(y)
at the Python prompt ? Give reasons for your answer.
What are the possible outcome(s) executed from the following code? Also specify the maximum and minimum values that can be assigned to variable NUMBER.
STRING = "CBSEONLINE" NUMBER = random.randint(0, 3) N = 9 while STRING[N] != 'L' : print(STRING[N] + STRING[NUMBER] + '#', end = '') NUMBER = NUMBER + 1 N = N - 1
- ES#NE#IO#
- LE#NO#ON#
- NS#IE#LO#
- EC#NB#IS#
Consider the following code :
import random print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10))
Find the suggested output options 1 to 4. Also, write the least value and highest value that can be generated.
- 102 105 104 105
- 110 103 104 105
- 105 107 105 110
- 110 105 105 110
Consider the following package
music/ Top-level package ├── __init__.py ├── formats/ Subpackage for file format │ ├── __init__.py │ └── wavread.py │ └── wavwrite.py │ ├── effects/ Subpackage for sound effects │ └── __init__.py │ └── echo.py │ └── surround.py │ └── reverse.py │ └── filters/ Subpackage for filters ├── __init__.py └── equalizer.py └── vocoder.py └── karaoke.py
Each of the above modules contain functions play(), writefile() and readfile().
(a) If the module wavwrite is imported using command import music.formats.wavwrite. How will you invoke its writefile() function ? Write command for it.
(b) If the module wavwrite is imported using command from music.formats import wavwrite. How will you invoke its writefile() function ? Write command for it.