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.pswp.cn/news/376945.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376945.shtml
英文地址,請注明出處:http://en.pswp.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…