- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想创建一个随机数生成代码,输入“计数”数,即它在程序中被调用的次数。我想要做的是对于函数的偶数次调用,我想创建 2 个随机数,它们是“y1,y2”但是 我只想输出 y1 并且 将 y2 保存为函数的下一次调用。 所以在奇数次调用时,函数将直接输出上一次调用的 y2。感谢帮助。到目前为止的代码:
import random
import math
def gaussianRandom ( count):
count += 1
if count%2 == 0:
while (1):
x1 = random.uniform(-1, 1)
x2 = random.uniform(-1, 1)
r = x1**2 + x2**2
if (r < 1):
break
y1 = x1 * math.sqrt( (-2 * math.log(r)) / r )
y2 = x2 * math.sqrt( (-2 * math.log(r)) / r )
return y1
最佳答案
函数可以有属性。
import random
import math
def gaussianRandom ( count):
count += 1
if count%2 == 0:
while (1):
x1 = random.uniform(-1, 1)
x2 = random.uniform(-1, 1)
r = x1**2 + x2**2
if (r < 1):
break
y1 = x1 * math.sqrt( (-2 * math.log(r)) / r )
gaussianRandom.y2 = x2 * math.sqrt( (-2 * math.log(r)) / r )
print(gaussianRandom.y2)
return y1
gaussianRandom.y2 = 99
print "y2", gaussianRandom(3)
print gaussianRandom(3)
print gaussianRandom(3)
y2 0.919282832355
-0.0887376744533
y2 -1.71553385287
0.422645022058
y2 -0.0668389339817
0.600351205084
关于 python : How to store a value generated in a function for the second call of that function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26482397/
我是 Julia 的新手。 我主要用python编程。 在 python 中, 如果你想迭代大量的值, 通常构造一个所谓的生成器来节省内存使用。 这是一个示例代码: def generator(N):
这个问题很奇怪。我试图直接在 build.gradle 中添加一个字符串,因为我需要它来使用 Tray 库 ( https://github.com/grandcentrix/tray )。在我的第一
我正在尝试在我的新 symfony3 项目中使用生成 CRUD 功能。我创建了一个名为 AdminBundle 的 bundle ,其中包含生成器、实体测试以及生成器,所有这些都处理得很好。我检查了我
我尝试理解生成器,但我发现了一个我无法遵循的示例。 // First Generator function* Colors () { yield "blue"; yield* MoreColo
我将制作一款完全由程序生成的空间/交易/战斗游戏。但是,我知道将整个星系的所有细节存储在内存中是不可行的。因此,我一直认为我可以使用种子来生成太阳系,并且从该太阳系,您可以使用跳跃门前往其他太阳系。问
我在 Eclipse Helios 中使用 MyBatis Generator (MyBatis Generator 1.3.1.201101032122),但每次我自动生成持久类时,生成器都会删除属
@GenerateInterface class A {} @GenerateInterface class B { void setA(IA a) {} } 我的注释处理器应该生成这些接口(in
我刚刚在一个空目录中安装了 yeoman,它打印出一个错误。这就是我所做的: npm i yo -g npm i generator-webapp -g 之后我抛出一个错误: require('yeo
我正在使用 NReco PDFGenerator 从 HTML 字符串创建 PDF 文档。当表格被分页符拆分时,表格标题与表格中的下一行重叠(见下图)。 有想法该怎么解决这个吗? 最佳答案 我发现这是
我有这个命名空间: namespace :manage do # Directs /manage/products/* to Manage::ProductsController
我有一个 Open API 3 规范的 yaml 文件,它有一些 x- 前缀的属性。我正在尝试使用 openapi-generator-cli 生成一个 Angular Typescript SDK。
我有一个返回生成器的函数。目前它使用yield from: function foo() { $generator = getGenerator(); // some other st
我选择Symfony2 docs 。据说添加 /** * @ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository
运行命令生成新的 rails 项目: $ rails generate controller home index 以上将创建四个新的 Rails 项目:generate、controller、hom
我们实际上已经将jvm内存增加到了256M,现在老年代看起来很小,但Perm Generation相当高,接近80%。通过 jstat 捕获的示例数据如下。高永久代意味着什么? Timestamp
class Invoice def Invoice.generate(order_id, charge_amount, credited_amount = 0.0) Invoice.new
我在写 this comparison为了帮助人们理解所有这些废话,目前看来,generator-angular 的好处和值(value)只是您使用 generator-angular-fullsta
我有一个包含以下代码段的 OpenAPI 规范文档(我无法控制): servers: - url: http://www.[someservice].com/api 我正在使用这个 OpenAPI
我正在使用 openapi-yaml 将 swagger 文件转换为开放的 API v3 文件。使用 Maven 生成器。 我想做的是将新文件直接放入某个目录。 但是会生成一些我不需要的其他文件,例如
我的生成器中有以下标准文件夹结构。我当前正在努力解决的任务是我目前有一个模板化的 _package.json ,我将其写入磁盘以用于主要生成。我想在编写的 package.json 中包含一个变量,它
我是一名优秀的程序员,十分优秀!