- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我尝试利用 Fisher-Yates 算法对数组的元素进行洗牌。然后我想将这个“洗牌”数组插入另一个数组中。我的目标是创建一个包含特定数量的“洗牌”数组的数组。
例如:
var myInput = [1, 2, 3, 4, 5];
我希望我的输出是这样的:
myOutput = [[1, 3, 5, 2, 4], [2, 5, 1, 3, 4], [5, 3, 1, 4, 2]];
所以问题发生在我运行我的函数之后。我提供了一个数组,它输出相同的数组,其中的元素按其应有的方式“洗牌”。我在循环中运行此函数,假设进行五 (5) 次迭代,并且每次迭代时,“洗牌”数组都会被推送到我的输出数组。然而,我的最终输出数组最终得到五 (5) 个相同的“洗牌”数组,而不是五个不同的数组。由于某种原因,它似乎充满了我循环的最后一次迭代中的“洗牌”数组。
这是我的代码:
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
};
return array;
};
var myInput = [1, 2, 3, 4, 5];
var myOutput = [];
for (i=0; i<5; i++){
var shuffledArr = shuffle(myInput);
console.log(shuffledArr);
myOutput.push(shuffledArr);
}
console.log(myOutput);
就像我说的,myOutput 最终成为一个五 (5) 个元素数组,每个元素都是从循环的最终迭代中推送的数组。在循环中,当 shuffledArr 变量记录到控制台时,它肯定与推送到我的输出数组的内容不同。
有什么想法吗?我对此感到非常困惑。我假设 Fisher-yates 算法中的某些内容导致了该问题。
最佳答案
您每次都会打乱相同的数组引用,并使用对同一 myInput
数组的 5 个引用填充较大的数组。
对任何一个引用的更改都会影响所有引用,因为它们都指向同一物理数组
使用 slice()
每次迭代创建一个副本,这样每次实际上都有一个新数组
var myInput = [1, 2, 3, 4, 5];
var myOutput = [];
for (i=0; i<5; i++){
var shuffledArr = shuffle(myInput.slice());
myOutput.push(shuffledArr);
}
console.log(myOutput);
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
};
return array;
};
简化示例:
var a = [1,2];
b = a; // not a copy of the [1,2] array ... is reference to it
b[0] = 99;
console.log(a) // [99,2] ...same as `b`
关于javascript - 使用 Fisher-Yates shuffle 推送重复数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043349/
我正在尝试复制以下 while 循环,但它们交错我的“卡片”的方式有所不同。 这是我当前有效的 while 循环,我的目标是重新创建此循环,但颠倒两个 card_force 数组的顺序: while
我有以下 Spark 作业,试图将所有内容保留在内存中: val myOutRDD = myInRDD.flatMap { fp => val tuple2List: ListBuffer[(St
我最近开始学习 tensorflow。 我不确定是否有区别 x = np.array([[1],[2],[3],[4],[5]]) dataset = tf.data.Dataset.from_ten
我想重置 pyspark 代码中的 spark.sql.shuffle.partitions 配置,因为我需要加入两个大表。但是以下代码在最新的spark版本中不起作用,错误说“xxx中没有方法“se
我只是想验证我对这些参数及其关系的理解,如果我错了请通知我。 mapreduce.reduce.shuffle.input.buffer.percent 告诉分配给 reducer 的整个洗牌阶段的内
假设我的数据库中有 10 个项目正在尝试洗牌,我如何更改当前的代码,以便每次从数据库中提取一个名称时,一次显示一个名称,而不是全部显示一次? $con = mysqli_connect("XXX",
1.概述 转载:Flink Remote Shuffle 开源:面向流批一体与云原生的 Shuffle 服务 2.开源 作为支持 Flink 流批一体与云原生的重要组成部分,Flink Remote
这个 fiddle 演示了我的问题:https://jsfiddle.net/petebere/fhg84je2/ 我想确保每次用户单击按钮时都会显示数组中的随机元素。问题是,有时进行新的混洗时,新混
对于那些了解情况的人来说,这应该是一个足够简单的问题 - 为什么我会在控制台中收到此错误?我尝试按照 Shuffle homepage 上“用法”下显示的代码进行操作但我认为该页面忽略了包含开始使用该
在下面的 Spark admin 在端口 8080 上运行的屏幕截图中: 此代码的“随机读取”和“随机写入”参数始终为空: import org.apache.spark.SparkContext;
docs说“所有排列的发生概率大致相等。”但我不知道这是否包括返回相同订单的可能性(无论多么小)。我有一个方法(见下文),在两次测试运行期间,列表以原始顺序返回,也许……其他因素可能有问题,比如可能已
我有一份处理大量数据的工作。此作业经常运行而没有任何错误,但偶尔会引发此错误。我正在使用 Kyro Serializer。 我正在使用 yarn 集群运行 Spark 1.2.0。 完整的堆栈跟踪在这
我正在 EC2 集群上部署 Spark 数据处理作业,该作业对于集群来说很小(16 个核心,总共 120G RAM),最大的 RDD 只有 76k+ 行。但是中间严重倾斜(因此需要重新分区)并且每
打乱数据的 spark sql 聚合操作,即 spark.sql.shuffle.partitions 200(默认情况下)。当 shuffle partition 大于 200 时,性能会发生什么变
打乱数据的 spark sql 聚合操作,即 spark.sql.shuffle.partitions 200(默认情况下)。当 shuffle partition 大于 200 时,性能会发生什么变
当在 Python 3 中使用 random 模块 random.shuffle(list(range(n))) 时,但是 random.shuffle(range( n)) 没有。 为什么会这样?
当我尝试在 pycaret 中训练某些东西时,我收到此错误消息 ValueError: Setting a random_state has no effect since shuffle is Fa
我正在以推测模式运行 Spark 作业。我有大约 500 个任务和大约 500 个 1 GB gz 压缩文件。我不断地进入每项作业,对于 1-2 个任务,附加错误,然后它会重新运行数十次(阻止作业完成
作为Django中关键字云函数的一部分,我正在尝试输出字符串列表。是否有模板过滤器,可让您随机播放列表中的项目?我认为这很简单,但是我在官方文档中找不到任何适用的过滤器。 最佳答案 制作自己的东西很简
同时思考this问题并与参与者交谈后,出现了这样的想法:对一组有限的明显有偏见的随机数进行洗牌,使它们变得随机,因为你不知道它们被选择的顺序。这是真的吗?如果是的话,有人可以指出一些资源吗? 编辑:我
我是一名优秀的程序员,十分优秀!