注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
请求高手帮帮忙, 把这题给刷了。 来在 UC B HW08 的 题目
Question 7: Linear Congruential GeneratorA common method of producing pseudo-random numbers is by means of thefollowing recurrence relation:
- R0 = seed value
- Ri+1 = (a*Ri + c) % n where Ri denotes the ith pseudo-random number in the stream; a, c, and n are constant integers, and seed value is some initial value provided by the user or chosen automatically by the system.
Define a function that returns a stream of random numbers that usesthis linear-congruential formula.
from operator import add, mul, moddef make_random_stream(seed, a, c, n): """The infinite stream of pseudo-random numbers generated by the recurrence r[0] = SEED, r[i+1] = (r * A + C) % N. Your solution must not use any lambdas or def's that we have not supplied in the skeleton. >>> s = make_random_stream(25, 29, 5, 32) >>> stream_to_list(s, 10) [25, 26, 23, 0, 5, 22, 3, 28, 17, 18] >>> s = make_random_stream(17, 299317, 13, 2**20) >>> stream_to_list(s, 10) [17, 894098, 115783, 383424, 775373, 994174, 941859, 558412, 238793, 718506] """ "*** YOUR CODE HERE ***"Your solution must use [i]only the functions defined in the skeleton, withoutdefining any additional ones.Likewise, any lambda expressions should contain only calls to thesefunctions.
Use OK to test your code:
python3 ok -q make_random_stream
感谢!
|