- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
对于多人游戏,我需要在所有参与的设备上生成相同的随机数序列。显然,这是通过在所有设备上使用相同的种子来实现的。当在本应在所有设备上生成相同序列的例程之外调用 random()
时,就会出现问题。因此,在此例程中,我尝试使用 setstate
来保存状态数组。据我了解 man random(3)
,在此例程之外调用 random()
不应更改序列。
但是,以下代码不会为运行 1 和运行 2 产生相同的输出:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[])
{
char state[256];
printf("Run 1 : ");
initstate(123, state, 256);
for (int i=1; i < 10; i++) {
printf("%ld ", random());
}
printf("\nRun 2 : ");
initstate(123, state, 256);
for (int i=1; i < 10; i++) {
setstate(state); // load preserved state
printf("%ld ", random());
*state = *setstate(state); // preserve state
random(); // this simulates a call to random() from outside
}
printf("\n");
return 0;
}
Run 1 : 1597493280 1407130876 1753502901 1965067074 377602131 83146350 274392949 1718024305 1016176754
Run 2 : 1597493280 537479855 1611694138 941096776 83164437 1459338036 1256894804 1618690717 1091902527
Program ended with exit code: 0
有人知道为什么吗?或者,也许还有另一种方法可以达到预期的效果?
备案:使用 Xcode 5 在 OS X 和 iOS 上运行。
编辑在 Arkku 和 minitech 的帮助下,以下更改使运行 2 的输出与运行 1 相同:
printf("\nRun 2 : ");
char otherstate[256];
initstate(123, state, 256);
for (int i=1; i < 10; i++) {
// preserve whatever state is currently active
// and set the current state to "my" state
memcpy(otherstate, setstate(state), 256);
printf("%ld ", random());
// switch back to the other state
memcpy(state, setstate(otherstate), 256);
// now this call does not affect the sequence that get's printf'd
random();
}
printf("\n");
最佳答案
在第二个循环中,您调用了 random()
而没有打印出值。此外,setstate
行没有用;每次都使用相同的数组 (state
),因此状态会不断变化。这两个序列变得完全相同:
for (int i = 1; i <= 2; ++i) {
printf("\nRun %d: ", i);
initstate(123, state, sizeof(state));
for (int i=1; i < 10; i++) {
printf("%ld ", random());
}
}
如果您需要在生成一些随机数后保存状态,然后返回到相同状态,您必须在不同状态数组之间交替,例如:
char state[256], state2[256];
initstate(123, state, sizeof(state));
char *saved = initstate(123, state2, sizeof(state2));
for (int i=1; i < 10; i++) {
saved = setstate(saved);
printf("%ld ", random());
saved = setstate(saved);
(void) random();
(void) random();
}
每次调用 initstate
或 setstate
都会返回一个指向previous 状态数组的指针。要返回该状态,您需要调用 setstate
并将其作为参数并将返回的指针存储在某处(可能指向相同的指针,如此处所示)。您还需要有两个不同的状态数组,否则您将一遍又一遍地设置相同的数组——在上面的示例中,saved
的初始值来自 second调用initstate
,即第一次调用时设置的状态数组。 (第一次调用可能会返回一些内部数组,但为了获得与所需数组大小一致的结果,我认为最好自己创建两个数组。)
关于c - 使用 setstate(3) 不会产生预期的随机数序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22029134/
我可以在传递给 this.setState() 的回调定义中显式调用 this.setState() 吗? this.setState( { openA:true }, () => {
我有一个从 api 获取数据的场景。在这种情况下,每当我从商店获取新值时,我的 componentWillReceiveProps() 就会被触发。 componentWillReceiveProps
在这个 componentDidUpdate 方法中,在执行 setState 将引号设置为从 fetch 返回的内容后,我必须使用回调再次执行 setState 以将 randomQuoteInde
正如 another question 中向我指出的那样,如果我在 setState、randomQuoteIndex() 中有一个不带参数的函数调用,并且该函数使用该 setState 中设置的状态
问题:当我使用 this.setState 并在回调中输出状态时,它根本不会改变,但是当我将 setstate 嵌套在 setstate 中时,它就会正常工作。 例子:这行不通- this.setSt
这个问题在这里已经有了答案: Why does setState take a closure? (2 个答案) 关闭 4 年前。 我对这两者的区别还是有点迷惑 isBusy = false;
当我使用 setState () 文本出现在调试控制台中.. setState() callback argument returned a Future. The setState() method
这个问题已经有答案了: Are 'Arrow Functions' and 'Functions' equivalent / interchangeable? (4 个回答) React - unca
之间的区别 - this.setState({value: 'xyz', name: 'john', color: 'orange'}) 对比 setValue('xyz'); setName('jo
之间的区别 - this.setState({value: 'xyz', name: 'john', color: 'orange'}) 对比 setValue('xyz'); setName('jo
我在运行代码时收到以下警告: Line 48: Do not mutate state directly. Use setState() react/no-direct-mutation-state
我是 React 的新手,我注意到我们使用 this.setState() 而不是 super.setState() 请我清楚地解释为什么我们用它来调用父类(super class)方法??示例: c
我一生都无法在我的 react 组件中传递这个 TypeError 。我已阅读所有相关主题并实现了几乎所有建议的修复,但均无济于事。 import React, { Component } from
我知道这可能是一个 JavaScript 问题而不是 React 问题,但我无法理解 React setState 的上述签名。 函数参数列表中的方括号和逗号有什么作用? 我知道如何将它与更新程序一起
是否可以在this.setState的回调中调用this.setState? 我正在制作一个 Roguelike Dungeon 并有一个设置,其中在 this.setState 的回调中使用了一个辅
我正在使用 react-navigation 进行路由。在一个组件上,我尝试设置 navigationOptions 以显示汉堡包按钮以打开和关闭侧边栏(抽屉)。所以 onPress 我试图为我的侧边
在之前的 React 版本中,我们可以在状态更改后执行代码,方法如下: setState( prevState => {myval: !prevState.myval}, () => { co
有一个 react 问卷组件,它将选定的答案添加到 this.state = {answer: []} 中,效果很好。同时,当用户更新答案时,它会添加为另一个对象。当questionID相同时,是否有
我正在使用 WebSocket 与我的服务器进行通信,并在我的 handleSubmit() 函数上输入一些值,并在此基础上与服务器进行通信并将我的状态更新为数据从 ws 收到。所以,第一次,一切都工
componentDidMount(prevProps, prevState, prevContext) { let [audioNode, songLen] = [this.refs.aud
我是一名优秀的程序员,十分优秀!