fisher-yates_使用Fisher-Yates随机播放算法以O(n)时间随机播放给定数组

fisher-yates

Example:

例:

Say the input array is 
[1, 2 3, 4, 5 6, 7]
After reshuffling it can be anything like
[4, 3, 7, 2, 1, 5, 1]

Our goal is that the reshuffling should be as random as possible.

我们的目标是,改组应尽可能地随机。

There is a standard algorithm named Fisher-Yates algorithm which shuffles the array in linear times.

有一个名为Fisher-Yates算法的标准算法,可以在线性时间内对数组进行随机排序。

The idea is to start from an end

这个想法是从头开始

Say we are string from the right end and the array size is n

假设我们是从右端开始的字符串,数组大小为n

Now, pick the end element which will be the nth element, and pick up another element randomly from the range [0, n-1]. Then swap the elements and shrink the right end. Thus after this step, a[n-1](the rightmost element) is fixed. Continue the same until we reach the left end.

现在,选择将成为第n个元素的end元素,并从[0,n-1]范围内随机选择另一个元素。 然后交换元素并缩小右端。 因此,在此步骤之后, a [n-1] (最右边的元素)被固定。 继续同样操作,直到到达左端。

The detailed algorithm will be,

详细的算法将是

For i=n-1 to 1
Pick and element in the range [0,i-1] randomly
Swap the randomly picked element with a[i]
Decrement i
End for loop

Since, it's random we can't do dry run

因为这是随机的,所以我们不能空运行

But still to illustrate lets pick elements randomly as per thought

但仍需说明让我们根据想法随机选择元素

So initially,
Array is
[1, 2 3, 4, 5 6, 7]
So i=6
Say, we picked up 4th (0-indexed) element randomly (in the range [0-5])
Swap them
So array is now
[1, 2, 3, 4, 7, 6, 5]
So 5 is now fixed and is guaranteed to stay 
there after the whole shuffling completes
On the next iteration
i will be 5 and we will continue similar way until I becomes 1

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
//reference to array is passed as we need 
//to update the array within the function
void reshuffle(vector<int>& arr, int n)
{
srand(time(0));
for (int i = n - 1; i >= 1; i--) {
//j will be a random no with in range 0 to i-1
int j = rand() % i;
//swap ith index with jth
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main()
{
cout << "input array size\n";
int n;
cin >> n;
cout << "Input array elements \n";
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
reshuffle(arr, n);
cout << "After reshuffling, printing the array\n";
for (auto it : arr)
cout << it << " ";
cout << endl;
return 0;
}

Output:

输出:

input array size
6
Input array elements
12 34 32 56 48 11
After reshuffling, printing the array
56 48 12 11 32 34

Real-life applications:

实际应用:

Creating an application which will suggest movie/songs from a list randomly. But each time it would suggest a unique movie only.

创建一个可以从列表中随机建议电影/歌曲的应用程序。 但是每次都会只推荐一部独特的电影。

In this case, what we will do is we will show the ith indexed movie after each iteration. As the ith indexed one is never going to come again in the reshuffling as that's being fixed every time. That means we will recommend ith song/movie after the swap is done at each stage.

在这种情况下,我们要做的是在每次迭代后显示第i 索引的电影。 由于第i 被索引的索引永远不会在改组中再次出现,因为每次都固定。 这意味着在每个阶段完成交换后,我们将推荐第i首歌曲/电影。

Go through the below link to understand the detailed problem and solution.

浏览以下链接以了解详细的问题和解决方案。

翻译自: https://www.includehelp.com/data-structure-tutorial/shuffle-a-given-array-in-o-n-time-using-fisher-yates-shuffle-algorithm.aspx

fisher-yates

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376945.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[分享]一些在 WPF/Silverlight 中应用 MVVM 模式时可能会有点用途的代码

想来这个博客也已经有很久没更新过了&#xff0c;新年新气象&#xff0c;现在就开始写新内容吧。 最初的起因 在最近的几个月中我做的开发总是要跟 XAML 打交道&#xff0c;也就是 WPF 啊&#xff0c;Silverlight 啊&#xff0c;WF 啊这些。 在进行 WPF 和 Silverlight 开发的…

手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。...

1、拍照裁剪后 点击EditText会弹出输入法&#xff0c;却不能输入。可是点击点一EdtiText就能够输入了&#xff0c;所以我就写了一个看不见的EdtiText&#xff0c;切换焦点&#xff0c;这样就攻克了这个奇怪的这问题&#xff0c;应该是android内部的问题。 这是网络一个牛人留下…

Redis一个命令请求从发送到完成的步骤以及初始化服务器步骤

一个命令请求从发送到完成的步骤 如下&#xff1a; 1、客户端将命令请求发送给服务器 当用户在客户端中键入一个命令请求时&#xff0c;客户端会将这个命令请求转换成协议格式&#xff0c;然后通过连接到服务器的套接字&#xff0c;将协议格式的命令请求发送给服务器。 2、服…

c打印行号和函数_使用C中的函数名称,行号从任何函数打印错误消息

c打印行号和函数Sometimes, it is necessary to print some message on logic failure or anytime with the function name and line number, so that program can be debugged and fixed the issue. 有时&#xff0c;有必要在逻辑故障时或在任何时候使用功能名称和行​​号打印…

Linux SPI框架

水平有限&#xff0c;描述不当之处还请指出&#xff0c;转载请注明出处http://blog.csdn.net/vanbreaker/article/details/7733476 Linux的SPI子系统采用主机驱动和外设驱动分离的思想&#xff0c;首先主机SPI控制器是一种平台设备&#xff0c;因此它以platform的方式注册进内…

dbms标识符无效_DBMS中的嵌套查询,相关的嵌套查询和集合比较运算符

dbms标识符无效嵌套查询 (Nested Queries) A query embedded in a query. This type of relation is termed as Nested Query and the Embedded Query is termed as a subquery. 查询中嵌入的查询。 这种类型的关系称为嵌套查询&#xff0c;而嵌入式查询称为子查询。 For exam…

重构——解决过长参数列表(long parameter list)

目录1、Replace Param with Query2、Preserve Whole Object3、Introduce Param Object4、Remove Flag Argument5、Combine Functions into ClassReference当我们需要在超长函数中提炼子函数时&#xff0c;如果函数内有大量的参数和临时变量&#xff0c;这将会对函数的提炼形成很…

C# 点点滴滴: out和ref

用c#很长一段时间了&#xff0c;不过基本是啥都不会&#xff0c;当C用的&#xff0c;作为写单片机的&#xff0c;还是真心觉得C比较亲切&#xff0c;呵呵。 不过总是要进步啊&#xff0c;慢慢积累呗&#xff0c;这次是写一个CAN的上位机模板出来&#xff0c;以后的项目就要彻底…

css控制图片最宽 最高值

.content img{width:expression_r(this.width > 500 && this.height < this.width ? 500:true);max-width:500px;height:expression_r(this.height >500 ? 500:true);max-height:500px; }转载于:https://www.cnblogs.com/panlin/archive/2013/01/06/2848017…

踩踩踩

http://china.findlaw.cn/laodongfa/ctjg/cy/cybc/ 二、合法裁员经济补偿标准的计算 按照《劳动合同法》第四十七条规定&#xff0c;经济补偿按劳动者在本单位工作的年限&#xff0c;每满一年支付一个月工资的标准向劳动者支付。六个月以上不满一年的&#xff0c;按一年计算;不…

c# 字节十六进制转十进制_用C中的十进制,八进制和十六进制数字初始化字节数组...

c# 字节十六进制转十进制C中的字节数组 (byte array in C) In C programming language, an unsigned char type can be used to declare byte array in C programming language. An unsigned char can contain a value from 0 to 255, which is the value of a byte. 在C编程语…

从uptime、stress、mpstat、pidstat观察CPU密集型、IO密集型、进程密集型切换的系统性能

uptime dyydyy-Lenovo-ThinkBook-14-IIL:~$ uptime10:27:10 up 7 min, 1 user, load average: 1.32, 0.99, 0.49结果分别对应&#xff1a;当前时间、系统运行时间、当前用户数目、过去 1 分钟、5 分钟、15 分钟的平均负载(Load Average) 平均负载是指单位时间内&#xff0c…

解析和创建xml

http://www.cnblogs.com/Li-Cheng/p/3610474.html 转载于:https://www.cnblogs.com/mxw272618/p/3769900.html

python - VirtualEnv virtualenvwrapper

VirtualEnv 是什么 VirtualEnv用于在一台机器上创建多个独立的python运行环境&#xff0c;VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。 为什么要用 - 隔离项目之间的第三方包依赖&#xff0c;如A项目依赖django1.2.5&#xff0c;B项目依赖django1.3。- 为部署应用…

多台计算机共享内存_共享内存多处理器和指令执行| 计算机架构

多台计算机共享内存共享内存多处理器 (Shared Memory Multiprocessor) There are three types of shared memory multiprocessor: 共有三种类型的共享内存多处理器&#xff1a; UMA (Uniform Memory Access) UMA(统一内存访问) NUMA (Non- uniform Memory Access) NUMA(非统一…

htop与atop

htop htop使用详解–史上最强 atop Linux atop监控工具部署

js未看的文章

Web前端研发工程师编程能力飞升之路 在浏览器的背后&#xff08;一&#xff09; —— HTML语言的词法解析 组件化的前端开发流程 用js书写UI组件之js基础知识 GC与JS内存泄漏 蓝色理想之前端开发 w3c JavaScript Puzzlers react AngularJS入门教程 jQuery源码分析-如何做jQuery…

方法重写,隐藏在子类父类中的各种调用实践

一.子类和父类方法之间的关系 1.当子类和父类有方法完全相同的方法 namespace ConsoleApplication2 {class Program{static void Main(string[] args){B b new B();A a new A();A c new B();b.Show();a.Show();c.Show();Console.Read();}}public class A{public void Show()…

向量余弦值python_向量/矩阵的余弦值打印(元素明智的操作) 使用Python的线性代数

向量余弦值pythonPrerequisite: 先决条件&#xff1a; Defining a Vector 定义向量 Defining a Matrix 定义矩阵 Numpy is the library of function that helps to construct or manipulate matrices and vectors. The function numpy.cos(x) is a function used for generati…

centos 6.5网卡dhcp不能获得网关

环境:vmware centos6.5 添加两个虚拟网卡。一个自动获取ip(用于上网-桥接) 一个手动(与主机通信用于ssh-NAT)。 因为自已手动改了一下ifcfg-eth0里面的HWADDR地址。造成 eth0网卡不能识别。多出一个eth2的网卡。 配置eth2网卡&#xff0c;可以自动获取到ip地址 但用netstat -r…