Hard 随机洗牌函数 @CareerCup
第i个元素和index在[i,length-1]之间的一个数随机交换
package Hard; import CtCILibrary.AssortedMethods; /**
*
* Write a method to shuffle a deck of cards. It must be a perfect shuffle - in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. 译文: 写一个随机洗牌函数。要求洗出的52!种组合都是等概率的。 也就是你洗出的一种组合的概率是1/(52!)。假设已经给你一个完美的随机数发生器。
*
*/
public class S18_2 {
/* Random number between lower and higher, inclusive */
public static int rand(int lower, int higher) {
return lower + (int)(Math.random() * (higher - lower + 1));
} // 递归shuffle,先shuffle前i-1个元素,然后把第i个元素和前面的一个随机元素交换
public static int[] shuffleArrayRecursively(int[] cards, int i) {
if (i == 0) {
return cards;
} /* shuffle elements 0 through index - 1 */
shuffleArrayRecursively(cards, i - 1);
int k = rand(0, i); /* Swap element k and index */
int temp = cards[k];
cards[k] = cards[i];
cards[i] = temp; /* Return shuffled array */
return cards;
} public static void swap(int[] a, int n, int m){
int tmp = a[n];
a[n] = a[m];
a[m] = tmp;
} // 遍历数组,对每一个在index为i上的元素,和index在[i,n-1]之间的一个随机index选择的数交换
public static void shuffleArrayInteratively(int[] cards) {
for (int i = 0; i < cards.length; i++) {
int k = rand(i, cards.length-1); // 产生i到n-1间的随机数
swap(cards, i, k);
}
} public static void main(String[] args) {
int[] cards = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(AssortedMethods.arrayToString(cards));
shuffleArrayInteratively(cards);
System.out.println(AssortedMethods.arrayToString(cards));
}
}
Hard 随机洗牌函数 @CareerCup的更多相关文章
- Craking the coding interview 面试题:完美随机洗牌
给定一个序列,随机打乱这个序列,新产生的序列和任意一个序列产生的可能性是一样的,就是所谓的完美随机洗牌. 看下面的运行结果: 上面第一列是原数列,下面一行是新产生的打乱的数列. 基本思想:如果n-1个 ...
- 随机洗牌算法Knuth Shuffle和错排公式
Knuth随机洗牌算法:譬如现在有54张牌,如何洗牌才能保证随机性.可以这么考虑,从最末尾一张牌开始洗,对于每一张牌,编号在该牌前面的牌中任意一张选一张和当前牌进行交换,直至洗到第一张牌为止.参考代码 ...
- js 数组随机洗牌
//先定义一个某数值范围内的随机数 function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1) + ...
- OpenCL洗牌函数shuffle
在OpenCL中,经常会碰到会对向量的多个分量进行交叉运算的情况,比如 float4 d4; //input float scale; //input float2 mix_0 = mix((floa ...
- 洗牌函数[打乱数组的顺序] slice()的新运用 [原来arr.slice(start, end) 的start不是必需的]
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } functio ...
- 实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例)
实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例) 1.namedtuple:命名元组,可以创建一个没有方法只有属性的类 from collections import namedtup ...
- python 练习洗牌
生成随机数需要引入random模块,学习下random模块中常用的几个函数: random.random() 用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.un ...
- java斗地主扑克 扑克牌 洗牌 发牌 Collection 集合练习
package com.swift.poker; import java.util.ArrayList; import java.util.Collections; /*训练考核知识点:Collect ...
- [CareerCup] 18.2 Shuffle Cards 洗牌
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of ...
随机推荐
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- cocod2d-x 之 CCDirector、CCScene、CCSprite
CCDirector是控制游戏流程的主要组件. typedef enum { /// sets a 2D projection (orthogonal projection)2D投机模式 kCCDir ...
- dede列表标签递增数字生成
今天给人家调试程序,用到[field:global name=autoindex/ ] 调用指定频道.而频道前面需要加CSS代码,CSS代码正好是按序号排列的 忽然发现,是从0开始. 于是修改了一下代 ...
- JQuery select控件的相关操作
JQuery获取和设置Select选项方法汇总如下: 获取select 先看看下面代码: $("#select_id").change(function(){//code...}) ...
- Eclipse 代码自动补全
使用Eclipse开发Android时,发现代码补全功能太差,完全不像VS似的,输入一个字母就出现代码提示. 下面是在Eclipse中实现类似VS代码补全的方法: ①打开Eclipse->Win ...
- fread遇到1A则读取停止,发现是1A是文件截止符
fread遇到1A则读取停止,发现是1A是文件截止符 按二进制来读来则可以完全读取
- X-Plosives
uvaLive 3644:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pa ...
- varnish、squid、apache、nginx缓存的对比<转>
1.Squid,很古老的反向代理软件,拥有传统代理.身份验证.流量管理等高级功能,但是配置太复杂.它算是目前互联网应用得最多的反向缓存代理服务器,工作于各大古老的cdn上. 2.Varnish是新兴的 ...
- javascript 基础学习整理
1. javascript是动态语言,脚本语言,弱类型语言. 2. javascript代码在html文件中的位置安排,放在<body></body>内部与外部的区别.如何引用 ...
- 3 B. Lorry
题目大意:给你两种物品,每种物品有一个价值和花费,花费只有两种,一种花费为 , 一种花费为2.. 给你一个背包容量为v, 求当前容量下所能达到的最大价值. ====================== ...