日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fisher-yates_使用Fisher-Yates随机播放算法以O(n)时间随机播放给定数组 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

我們的目標(biāo)是,改組應(yīng)盡可能地隨機(jī)。

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

有一個(gè)名為Fisher-Yates算法的標(biāo)準(zhǔn)算法,可以在線性時(shí)間內(nèi)對(duì)數(shù)組進(jìn)行隨機(jī)排序。

The idea is to start from an end

這個(gè)想法是從頭開始

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

假設(shè)我們是從右端開始的字符串,數(shù)組大小為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.

現(xiàn)在,選擇將成為第n個(gè)元素的end元素,并從[0,n-1]范圍內(nèi)隨機(jī)選擇另一個(gè)元素。 然后交換元素并縮小右端。 因此,在此步驟之后, a [n-1] (最右邊的元素)被固定。 繼續(xù)同樣操作,直到到達(dá)左端。

The detailed algorithm will be,

詳細(xì)的算法將是

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

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

因?yàn)檫@是隨機(jī)的,所以我們不能空運(yùn)行

But still to illustrate lets pick elements randomly as per thought

但仍需說明讓我們根據(jù)想法隨機(jī)選擇元素

So initially, Array is[1, 2 3, 4, 5 6, 7]So i=6Say, 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 ++實(shí)現(xiàn):

#include <bits/stdc++.h> using namespace std;//reference to array is passed as we need //to update the array within the functionvoid 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-1int j = rand() % i;//swap ith index with jthint 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:

實(shí)際應(yīng)用:

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

創(chuàng)建一個(gè)可以從列表中隨機(jī)建議電影/歌曲的應(yīng)用程序。 但是每次都會(huì)只推薦一部獨(dú)特的電影。

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 個(gè)索引的電影。 由于第i 個(gè)被索引的索引永遠(yuǎn)不會(huì)在改組中再次出現(xiàn),因?yàn)槊看味脊潭ā?這意味著在每個(gè)階段完成交換后,我們將推薦第i首歌曲/電影。

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

瀏覽以下鏈接以了解詳細(xì)的問題和解決方案。

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

fisher-yates

總結(jié)

以上是生活随笔為你收集整理的fisher-yates_使用Fisher-Yates随机播放算法以O(n)时间随机播放给定数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。