博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c ++明明的随机数_从列表C ++程序中随机建议电影
阅读量:2528 次
发布时间:2019-05-11

本文共 3186 字,大约阅读时间需要 10 分钟。

c ++明明的随机数

Problem statement:

问题陈述:

Write an application code that will suggest movies from a list randomly and there won't be any repeat while suggesting the movies. That means the same movie won't be suggested twice though it will be done randomly. Input will be a movie list.

编写应用程序代码,以随机建议列表中的电影,并且在建议电影时不会重复。 这意味着尽管会随机播放同一部电影,但不会两次被推荐。 输入将是电影列表。

Prerequisite:

先决条件:

Example:

例:

Input:["D-DAY", "RAINCOAT", "OCTOBER","LUNCHBOX", "BARFI", "RAAZI","PIKU"]Movie suggestion list can be"BARFI""PIKU""RAAZI""OCTOBER""D_DAY""LUNCHBOX""RAINCOAT"

Solution

We can use Fisher-Yates random shuffling algorithm to solve this problem. Firstly, we need to create a map to store indexes of the movies as we will shuffle based on their indexes.

我们可以使用Fisher-Yates随机改组算法来解决此问题。 首先,我们需要创建一个地图来存储电影的索引,因为我们将根据电影的索引进行随机播放。

So the map will be:

因此,地图将为:

KEY	VALUE1	"D-DAY"2	"RAINCOAT"3	"OCTOBER"4	"LUNCHBOX"5	"BARFI"6	"RAAZI"7	"PIKU"

So our movie list will be converted as: [1,2,3,4,5,6,7]

因此,我们的电影列表将被转换为:[1,2,3,4,5,6,7]

Then we will shuffle using the Fisher-Yates algorithm

然后,我们将使用Fisher-Yates算法进行洗牌

At each step iteration, we will suggest movie[i]. Below is the detailed algorithm for suggesting movie randomly

在每一步迭代中,我们都会建议movie [i] 。 以下是随机建议电影的详细算法

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]    // since it's not going to be reshuffled again     // as we are decrementing i ,     // thus it's guaranteed that it won't be suggested twice     Recommend movie[a[i]]Decrement iEnd for loop

So, how this guarantees unique suggestion each time?

那么,如何保证每次的独特建议呢?

Because, we are fixing the ith element after the swap and decrementing i, so that the movie that got suggested never takes part again in swaps.

因为,我们在交换之后固定了 i 元素,并减小了i ,以便所建议的电影再也不会参与交换。

C++ Implementation:

C ++实现:

#include 
using namespace std;void recommend_movie_randomly(vector
movies){ srand(time(0)); map
mymap; int n = movies.size(); for (int i = 0; i < n; i++) { mymap[i] = movies[i]; } vector
arr(n); //stores the indexes for (int i = 0; i < n; i++) arr[i] = i; //shiffling randomly and suggesting movie //at each iteartion 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; //suggest the ith movie now cout << "Suggesting next movie...\n"; cout << mymap[arr[i]] << endl; }}int main(){ //input list vector
movie_list{ "D-DAY", "RAINCOAT", "OCTOBER", "LUNCHBOX", "BARFI", "RAAZI", "PIKU" }; cout << "Recommending movies randomly from the list\n"; recommend_movie_randomly(movie_list); return 0;}

Output:

输出:

Recommending movies randomly from the listSuggesting next movie...RAAZISuggesting next movie...OCTOBERSuggesting next movie...PIKUSuggesting next movie...D-DAYSuggesting next movie...RAINCOATSuggesting next movie...LUNCHBOX


Also tagged in:

还标记在:

翻译自:

c ++明明的随机数

转载地址:http://iyxzd.baihongyu.com/

你可能感兴趣的文章
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>