第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. C# RSA加密/解密

    RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法, ...

  2. Linux系统下分割tomcat日志

    在Linux系统下,tomcat日志catalina.out并不会像window系统下,按日期进行重写备份,因此在Linux系统下会造成日志文件过大的情况,本文介绍采用 cronolog工具进行如在w ...

  3. Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  4. mysql创建存储过程中的问题

    1.在创建存储过程成功后,使用call 存储过程名执行时报错: Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_gener ...

  5. Asp.net 实现图片缩放 无水印(方法二)

    public static System.Drawing.Image GetImage(string path) { try { if (path.StartsWith("http" ...

  6. dede操作成功信息提示修改

    函数修改 include/common.func.php 文件 function ShowMsg()函数

  7. windows7任务栏上的图标修复

    Technorati 标记: 疑难杂症   今天,我在使用Windows 7的时候,因为操作一些系统文件,发现桌面下角的个别正在运行的图标不见了,但是,我们如果再打开一个新程序,又会提醒你已经在运行了 ...

  8. iOS题

    对于语句NSString* testObject = [[NSData alloc] init];关于testObject是什么类型对象,以下说法正确的是: 答案:(A) A.编译时,NSString ...

  9. Java中传参的值传递和引用传递问题(转)

    今天遇到了一个java程序,需要用参数来返回值(虽然最后用另一种方法实现了),在网上看到这样一篇文章,很受启发. 本文章来自于http://hi.baidu.com/xzhilie/blog/item ...

  10. VS快捷键和技巧

    1. 怎样调整代码排版的格式? 选择:编辑->高级->设置文档的格式或编辑->高级->设置选中代码的格式. 格式化cs代码:Ctrl+k+f 格式化aspx代码:Ctrl+k+ ...