Code Studio: Reproducible Random Numbers
Random numbers are different every run, unless you set the seed. This is Practice: Random Number Generation as a program the tests can check.
The program reads a whole number from the Input tab. Use it as the seed with set.seed(), then:
- Print 5 numbers sampled without replacement from 1 to 10 with
sample(1:10, 5). - Print 2 letters sampled from
letters.
Print each with print(). Then run it twice with the same seed and confirm you get identical output, and once with a different seed.
Remove the set.seed() line and run it a few times. Why would the tests fail?
Random numbers are different every run, unless you set the seed. This is Practice: Random Number Generation as a program the tests can check.
The program reads a whole number from the Input tab. Use it as the seed with set.seed(), then:
- Print 5 numbers sampled without replacement from 1 to 10 with
sample(1:10, 5). - Print 2 letters sampled from
letters.
Print each with print(). Then run it twice with the same seed and confirm you get identical output, and once with a different seed.
Remove the set.seed() line and run it a few times. Why would the tests fail?