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: Shuffling a given array in O(n) time using Fisher-Yates algorithm

先决条件: 使用Fisher-Yates算法在O(n)时间内对给定数组进行混洗

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   VALUE
1   "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 i
End 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 <bits/stdc++.h>
using namespace std;
void recommend_movie_randomly(vector<string> movies)
{
srand(time(0));
map<int, string> mymap;
int n = movies.size();
for (int i = 0; i < n; i++) {
mymap[i] = movies[i];
}
vector<int> 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<string> 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 list
Suggesting next movie...
RAAZI
Suggesting next movie...
OCTOBER
Suggesting next movie...
PIKU
Suggesting next movie...
D-DAY
Suggesting next movie...
RAINCOAT
Suggesting next movie...
LUNCHBOX



Also tagged in: Synopsys

还标记在: Synopsys

翻译自: https://www.includehelp.com/icp/suggesting-movie-randomly-from-a-list-cpp-program.aspx

c ++明明的随机数

c ++明明的随机数_从列表C ++程序中随机建议电影相关推荐

  1. java操作数据库挂死_如何在Java程序中处理数据库超时与死?

    什么是数据库锁定与死锁 锁定(Locking)发生在当一个事务获得对某一资源的"锁"时,这时,其他的事务就不能更改这个资源了,这种机制的存在是为了保证数据一致性:在设计与数据库交互 ...

  2. java中什么时候应用异常_生产Java应用程序中的十大异常类型-基于1B事件

    java中什么时候应用异常 Pareto记录原理:97%的记录错误语句是由3%的唯一错误引起的 在最新的数据整理帖子之后,我们收到了很多反馈和问题,我们发现97%的记录错误是由10个唯一错误引起的 . ...

  3. java程序中用户名和密码_在Java应用程序中使用密码术

    java程序中用户名和密码 这篇文章描述了如何使用Java密码体系结构 (JCA),该体系结构使您可以在应用程序中使用密码服务. Java密码体系结构服务 JCA提供了许多加密服务,例如消息摘要和签名 ...

  4. 人脸识别客户端应用程序_如何在应用程序中使用功能识别设置人脸检测

    人脸识别客户端应用程序 by Rohit Ramname 由Rohit Ramname 如何在应用程序中使用功能识别设置人脸检测 (How you can set up face detection ...

  5. 数据透视表 筛选_筛选列表可见行中的数据透视表

    数据透视表 筛选 When you create a pivot table in Excel, it doesn't matter if there are filters applied in t ...

  6. python中的np.array函数_对列表numpy数组中的每个列表应用函数

    一些比较和时间测试:但请记住,这只是一个小例子.在In [106]: test_arr = np.array([['the', 'quick', 'brown', 'fox'], ['lorem', ...

  7. go 用 mysql web开发环境_简单讲解Go程序中使用MySQL的方法

    go官方仅提供了database package,database package下有两个包sql,sql/driver.这两个包用来定义操作数据库的接口,这就保证了无论使用哪种数据库,他们的操作方式 ...

  8. akka应用_处理Akka应用程序中的每个事件

    akka应用 这里的事件,那里的事件,到处都是事件. 发布有关检查每一项Akka事件最终都能找到归宿的信息. Akka和基于事件的React式应用程序是创建软件的新方法. 在当前基于Scala的项目中 ...

  9. python代码执行过程记录_详解python程序中记录日志的方法

    日志可以用来记录应用程序的状态.错误和信息消息,也经常作为调试程序的工具.它的重要性就不多说了,直接进入正题. python提供了一个标准的日志接口,就是logging模块.日志级别有DEBUG.IN ...

最新文章

  1. 将assembly包添加到自己的maven仓库
  2. python中copy怎么用_python中的拷贝copy模块怎么使用?
  3. 如何解决用伪元素点击下拉列表触发不了事件的问题
  4. Redis4.0.13 安装踩雷记录
  5. CDI services--Decorators(装饰器)
  6. 二十二 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别...
  7. 【博客项目】—登录功能实现( 四)
  8. Java程序低手之关于泛型(Generic)
  9. html获取xml的数据,xml格式获取值
  10. 计算机网络自顶向下方法
  11. Vue Canvas 实现电子签名 手写板
  12. Java高阶知识体系总结(一)
  13. 创建SSM项目框架搭建流程--史上最详细教程
  14. Java游戏实验报告_Java实验报告(实验三)
  15. [CF891D]Sloth
  16. android mmdd 时间问题
  17. 家用千兆路由器排行榜前十名_家用路由器排名前十名
  18. 微信小程序-轮播图的实现
  19. c++中获得对象类型 typeid 与 type_info
  20. python求和1到100_python等差数列求和公式前 100 项的和实例

热门文章

  1. kail利用msf工具对MS12-020漏洞进行渗透测试
  2. VUE v-bind绑定class和style
  3. Vue 生命周期LIFECYCLE是8个吗?
  4. 课时27.base(掌握)
  5. vue2.5.2版本 :MAC设置应用在127.0.0.1:80端口访问; 并将127.0.0.1指向www.yours.com ;问题“ Invalid Host header”
  6. json 对象 数组
  7. HTML5结合ajax实现文件上传以及进度显示
  8. 纯CSS3美化单选按钮radio
  9. border,padding,margin盒模型理解
  10. PHP中cookie和session的区别