查看: 3589| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

UCB CS61A 题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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

感谢!



上一篇:有人知道CS61b用的emacs是什么配置么?
下一篇:有人了解blockchain数据库吗?
🔗
 楼主| DavisXie 2017-4-15 04:26:05 | 只看该作者
全局:
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[i] * 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 ***"
回复

使用道具 举报

🔗
 楼主| DavisXie 2017-4-15 04:27:08 | 只看该作者
全局:
  1. <span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> add, mul, mod

  2. <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_random_stream</span><span class="hljs-params">(seed, a, c, n)</span>:</span>
  3.     <span class="hljs-string">"""The infinite stream of pseudo-random numbers generated by the
  4.     recurrence r[0] = SEED, r[i+1] = (r[i] * A + C) % N.  Your solution
  5.     must not use any lambdas or def's that we have not supplied in the
  6.     skeleton.
  7.     >>> s = make_random_stream(25, 29, 5, 32)
  8.     >>> stream_to_list(s, 10)
  9.     [25, 26, 23, 0, 5, 22, 3, 28, 17, 18]
  10.     >>> s = make_random_stream(17, 299317, 13, 2**20)
  11.     >>> stream_to_list(s, 10)
  12.     [17, 894098, 115783, 383424, 775373, 994174, 941859, 558412, 238793, 718506]
  13.     """</span>

  14.     <span class="hljs-string">"*** YOUR CODE HERE ***"<p style="line-height: 30px; text-indent: 2em;"></p></span>
复制代码
回复

使用道具 举报

🔗
YuanGao 2017-4-15 09:44:37 | 只看该作者
全局:
这个递归关系挺简单的吧, 最好还是自己试着写写看。61A跟一遍下来会很有收获的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表