Lab activity
Pseudorandom sequence analysis
The following code generates and print 10 random values:
conta = 0
while conta<10:
x = random.random();
print(x);
conta = conta + 1
Write a program that:
- takes in input a natural number
n
,
- prints
n
random values.
Modify the program to achieve, one at a time, the following objectives (only one sequence should be generated):
- print the sum of the generated values (the sum should be printed only once)
- print the maximum of the generated values (the maximum should be printed only once)
- print the minimum of the generated values (the minimum should be printed only once)
- decide whether the generated sequence contains at least one element greater than 0.9, (use a boolean variable ...)
- decide whether the generated sequence consists exclusively of elements greater than 0.11, (use a boolean variable to look for violations of the required condition ..)
- decide whether the generated sequence is increasing (to solve this point it is necessary to keep both the current value and the value generated in the previous iteration; at each iteration the current value is copied into the variable containing the value of the previous iteration ...)
- count the number of maximal increasing sequences. For example, the sequence
0.23 - 0.15 - 0.01 - 0.48 - 0.68 - 0.27 - 0.83
consists in 4 maximal increasing sequences:
0.23; 0.15; 0.01 - 0.48 - 0.68; 0.27 - 0.83;
(an increasing sequence ends as soon as soem kind of violation is encountered, or at the end of the sequence ...),
- find the length of the maximum increasing portion of the sequence (this point is particularly challenging): for example, if the generated sequence were 0.23 - 0.15 - 0.01 - 0.48 - 0.68 - 0.27 - 0.83, the maximum increasing portion would have length 3 (0.01 - 0.48 - 0.68)
- count the number of local maxima (a local maximum is an element that is preceded by a lower element and followed by a lower element. The first and last elements cannot be local maxima)