- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在尝试直接对二维数组的前 N 个元素执行部分 fisher-yates 洗牌,但没有成功。我知道我可以将 2D 数组转换为 1D 数组,执行随机播放然后将其转回,但如果可能的话我想避免这种情况。
我的实现中的问题是,虽然我没有证明,但我的数组每一行的最后一个元素没有正确打乱。事实上,假设我有一个 m*n
数组,其中第一个 N
等于 1,其余 m*n - N
是等于 0。当我对数组的前 N
元素执行随机播放时,大约 67% 的时间是每行末尾的元素:array[i][end]
等于 1,这在我看来太多次了。
这是我的实现以及可以运行以显示问题所在的驱动程序代码:
void swap(int **a, int i, int j, int iNew, int jNew)
{
int temp = a[i][j];
a[i][j] = a[iNew][jNew];
a[iNew][jNew] = temp;
}
void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a)
{
for (int i = 0; i < nbLines; i++)
{
for (int j = 0; j < nbColumns; j++)
{
swap(a, i, j, i+rand()%(nbLines-i), j+rand()%(nbColumns -j)); // swap element with random later element
n--;
if(n<=0)
break;
}
if(n<=0)
break;
}
}
int main(int argc, char const *argv[])
{
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, N = 10, k = 100000;
srand(time(NULL));
//Short example of a 5 by 5 array, with N = 10 first elements equal to 1
//that need to to be shuffled among all of its elements.
while(k > 0)
{
//allocate
N = 10;
int **a = (int**)malloc(sizeof(int*) * 5);
for(int i = 0; i < 5; i++)
a[i] = (int*)malloc(sizeof(int) * 5);
//initialize
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(N > 0)
a[i][j] = 1;
else
a[i][j] = 0;
N--;
}
}
//shuffle
fisher_yates_shuffle(10, 5, 5, a);
//count how many times the last element of each row is equal to 1.
if(a[1][4] == 1)
count1++;
if(a[2][4] == 1)
count2++;
if(a[3][4] == 1)
count3++;
if(a[4][4] == 1)
count4++;
//destroy memory allocated.
for(int i = 0; i < 5; i++)
free(a[i]);
free(a);
k--;
}
//print approximate results.
printf("%d %d %d %d\n", (count1 * 100)/100000, (count2 * 100)/100000, (count3 * 100)/100000, (count4 * 100/100000));
return 0;
}
我知道它看起来不太好,必须有更有效的方法来做到这一点。也许有一种不同的、同样有效的算法来打乱二维数组的前 N 个元素?
最佳答案
扩展我之前的评论,你可以在一个循环中洗牌你的“数组”:
void fisher_yates_shuffle(int n, int nbLines, int nbColumns, int **a)
{
for (int i = 0; i < n; i++)
{
int j = i + rand() % (nbLines * nbColumns - i);
swap(a, i / nbColumns, i % nbColumns, j / nbColumns, j % nbColumns);
}
}
请注意,在 C 中有更好的方法来实现和传递矩阵,并且您的交换函数应该返回 void
,而不是 int
。
关于c - 直接在二维数组中执行 fisher - yates shuffle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53329280/
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
许多书籍都使用下图来说明 Fisher 线性判别分析的思想(该图来自 Pattern Recognition and Machine Learning,第 188 页) 我想知道如何用 R(或任何其他
我需要对矩阵 n x m 进行 Fisher 精确检验。我已经搜索了几个小时,但只找到了一个示例代码,但它是用 Fortran 语言编写的。我一直在使用 Wolfram 工作,我快要完成了,但我错过了
给定数据矩阵 X.shape = (n, d) 和单个预测变量的估计值 y_pred.shape = (n,),可以计算Fisher Information 我 喜欢这样: V = diags
我最近在 Lisp 中实现了 Fisher 的线性判别 (FLD)。到目前为止,我一直在使用 dim(<10) 的样本,其中 FLD 立即执行的数量为 10。今天早上,我使用 dim(5) 和 104
是否可以使用 Fisher 精确检验的矢量化来优化此计算,如果可以,如何优化?当 num_cases 运行时很麻烦> ~1000000。 import numpy as np from scipy.s
鉴于简单 A/B 测试的结果...... A B clicked 8 60 ignored 192 1940 (即 A 4% 和 B 3% 的 session 率) ... R
现在我正在开发一套文字游戏作为自学的一种方式(并重新创建一些我最喜欢的文字游戏!)在一位“真正”学习编程的 friend 的帮助下,我们实现了一个很好的排列方法在我的一门课上。它正在查找 3 个及以上
我是一般编码的新手,现在已经使用 javascript 有一段时间了 - 对于我在发布此问题时可能犯的任何失误,提前致歉。我研究了 2 个小时的大部分时间,无法自己得出答案,所以我在这里注册了一个帐户
我知道F-Y和reservoir sampling都可以实现shuffle array。比如我们在一个m * n的扫雷板上部署k个炸弹。 我已经完成了示例代码: public int[][] init
我正在尝试在 SPOJ 上解决这个问题: http://www.spoj.pl/problems/FISHER/ 我想不出解决这个问题的办法。我在 topcoder
所以我目前正在使用 Actionscript 3.0 制作问答游戏,我想使用 Fisher-Yates 随机播放算法随机播放问题: 这是我的代码: var questions:Array = [1,2
我并行启动几个依赖于随机数的 C/C++ 程序。对这个话题还算陌生,听说过段时间应该做seed。 此外,我使用 Fisher Yates 算法获得具有唯一随机打乱值的列表。但是,并行启动程序两次会为两
我正在编写一个小型技术分析库,其中包含 TA-lib 中不可用的项目。我从在 cTrader 上找到的示例开始并将其与 TradingView 版本中的代码进行匹配。 这是来自 TradingView
我正在使用标准的 Fisher-Yates 算法随机洗牌数组中的一副牌。但是,我不确定这是否真的会产生真实世界洗牌后所有可能排列的真实分布。 V8 的 Math.random 只有 128 位的内部状
我的数据框看起来像这样: 595.00000 18696 984.00200 32185 Group1 935.00000 18356 1589.000
我正在尝试在模拟 i.i.d 上实现 Fisher Scoring。 Poisson 数据,但出现堆栈溢出错误。我从函数中做了一些简单的打印,发现第一次迭代后猜测值没有改变。 fs_pois <- f
根据维基百科和Java标准库的实现,shuffling https://en.wikipedia.org/wiki/Fisher–Yates_shuffle (Fisher Yates Shuffli
我有一个由~3k 调查人员进行的~50k 测量的数据框。 INVESTIGATOR_ID \\\ SAMPLE_ID \\\ MEASUREMENT1000 \\\ 38942
意识到当某些事情看起来好得令人难以置信时,我想我会提出这个问题,希望能清除任何小 Sprite 。我回顾了我能找到的几个相关主题,但我的问题仍然存在。 我对 Haskell 比较陌生,在我的实验中,我
我是一名优秀的程序员,十分优秀!