第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的更多相关文章

  1. Craking the coding interview 面试题:完美随机洗牌

    给定一个序列,随机打乱这个序列,新产生的序列和任意一个序列产生的可能性是一样的,就是所谓的完美随机洗牌. 看下面的运行结果: 上面第一列是原数列,下面一行是新产生的打乱的数列. 基本思想:如果n-1个 ...

  2. 随机洗牌算法Knuth Shuffle和错排公式

    Knuth随机洗牌算法:譬如现在有54张牌,如何洗牌才能保证随机性.可以这么考虑,从最末尾一张牌开始洗,对于每一张牌,编号在该牌前面的牌中任意一张选一张和当前牌进行交换,直至洗到第一张牌为止.参考代码 ...

  3. js 数组随机洗牌

    //先定义一个某数值范围内的随机数 function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1) + ...

  4. OpenCL洗牌函数shuffle

    在OpenCL中,经常会碰到会对向量的多个分量进行交叉运算的情况,比如 float4 d4; //input float scale; //input float2 mix_0 = mix((floa ...

  5. 洗牌函数[打乱数组的顺序] slice()的新运用 [原来arr.slice(start, end) 的start不是必需的]

    function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } functio ...

  6. 实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例)

    实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例) 1.namedtuple:命名元组,可以创建一个没有方法只有属性的类 from collections import namedtup ...

  7. python 练习洗牌

    生成随机数需要引入random模块,学习下random模块中常用的几个函数: random.random() 用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.un ...

  8. java斗地主扑克 扑克牌 洗牌 发牌 Collection 集合练习

    package com.swift.poker; import java.util.ArrayList; import java.util.Collections; /*训练考核知识点:Collect ...

  9. [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 ...

随机推荐

  1. corejava-chap01

    <java是什么:>Programming language 程序语言Development environment 开发环境Application environment 应用环境Dep ...

  2. [LeetCode OJ] Best Time to Buy and Sell Stock I

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. jQuery实现公告文字左右滚动的代码。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. C# RSA

    using System; using System.Security.Cryptography; using System.Text; class RSACSPSample { static voi ...

  5. 在Lufylegend中如何设置bitmap或者sprite的缩放和旋转中心

    最近两天有个lufylegend游戏引擎群的群友需要做一个项目,其中要解决的需求是:获取照相机拍摄的图片,根据图片的EXIF信息让图片显示为“正常”情况,并且需要给图片添加一些事件侦听.何为正常呢?就 ...

  6. js获取屏幕(设备)宽高

    平常获取设备的宽高无非就那几 <script language="javascript"> var h = ""; h += " 网页可见 ...

  7. Python自动化运维之31、Tornado框架

    Tornado 官网:http://www.tornadoweb.org/en/stable/ Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本. ...

  8. 总结:S5PV210时钟系统

    在数据手册<S5PV210_UM_REV1.1>中的section 02_system/3 CLOCK CONTROLLER(354页)   一.时钟域 在S5PV210的SoC中,时钟系 ...

  9. +=与join的性能测试

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. sql 学习之 group by 及 聚合函数

    1.在使用 GROUP BY 子句时,Select列表中的所有列必须是聚合列(SUM,MIN/MAX,AVG等)或是GROUP BY 子句中包括的列.同样,如果在SELECT 列表中使用聚合列,SEL ...