算法同上题

package Hard;

import CtCILibrary.AssortedMethods;

/**
* Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. 译文: 写一个函数,随机地从大小为n的数组中选取m个整数。要求每个元素被选中的概率相等。
*
*/
public class S18_3 { /* Random number between lower and higher, inclusive */
public static int rand(int lower, int higher) {
return lower + (int)(Math.random() * (higher - lower + 1));
} public static void swap(int[] a, int n, int m){
int tmp = a[n];
a[n] = a[m];
a[m] = tmp;
} /* pick M elements from original array, using only elements 0 through i (inclusive).*/
public static int[] pickMRecursively(int[] original, int m, int i) {
if (i + 1 < m) { // Not enough elements
return null;
} else if (i + 1 == m) { // Base case -- copy first m elements into array
int[] set = new int[m];
for (int k = 0; k < m; k++) {
set[k] = original[k];
}
return set;
} else {
int[] set = pickMRecursively(original, m, i - 1);
int k = rand(0, i);
if (k < m) {
set[k] = original[i];
}
return set;
}
} /* pick M elements from original array. Clone original array so that
* we don’t destroy the input. */
public static int[] pickMRandomly(int[] original, int m) {
for (int i = 0; i < m; i++) {
int k = rand(i, original.length-1); // 产生i到n-1间的随机数
swap(original, i, k);
}
int[] subset = new int[m];
for(int i=0; i<m; i++){
subset[i] = original[i];
}
return subset;
} public static void main(String[] args) {
int[] cards = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(AssortedMethods.arrayToString(cards));
int[] set = pickMRandomly(cards, 4);
System.out.println(AssortedMethods.arrayToString(set));
}
}

Hard 随机选择subset @CareerCup的更多相关文章

  1. python3 selenium 随机选择同一类型下的某一个元素

    使用场景: 如上图所示,有时候,我们测试的时候,不会每个方向都选择一遍,也不能每次都选择一个方向,这个时候就需要每次运行用例的时候,随机选择一个方向来测试 使用方法: random.randint() ...

  2. python random从集合中随机选择元素

    1.使用python random模块的choice方法随机选择某个元素 from random import choice foo = ['a', 'b', 'c', 'd', 'e'] print ...

  3. ISLR系列:(4.1)模型选择 Subset Selection

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  4. Java-Selenium,获取下拉框中的每个选项的值,并随机选择某个选项

    今天逛51testing,看见有人问这个问题.现在以Select标签为例. 1.首先看页面中的下拉框,如图: 2.F12查看页面源代码,如下 <select class="form-c ...

  5. random os 序列化 模块模块 随机选择

    # 1 random 模块 随机选择# import random#随机取小数# ret = random.random() #空是0到1之间的小数字# print(ret)# # 0.0799728 ...

  6. 使用Numpy验证Google GRE的随机选择算法

    最近在读<SRE Google运维解密>第20章提到数据中心内部服务器的负载均衡方法,文章对比了几种负载均衡的算法,其中随机选择算法,非常适合用 Numpy 模拟并且用 Matplotli ...

  7. php array_rand()函数从数组中随机选择一个或多个元素

    php使用array_rand()函数从数组中随机选择一个或多个元素的方法. 使用array_rand() 函数从数组中随机选出一个或多个元素,并返回.  array_rand(array,numbe ...

  8. python random 随机选择操作

    # -*- coding:utf-8 -*- import random arr = ['A','B','C','D','E','F'] #生成(0.0, 1.0)的随机数 print random. ...

  9. python: 随机选择

    想从一个序列中随机抽取若干元素,或者想生成几个随机数. random 模块有大量的函数用来产生随机数和随机选择元素.比如,要想从一个序列中随机的抽取一个元素,可以使用random.choice() : ...

随机推荐

  1. 关于 ASP.NET 验证码

    Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...

  2. angular.js 字符串

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  3. SGU 194. Reactor Cooling(无源汇有上下界的网络流)

    时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...

  4. 【BZOJ1036】【LCT版】树的统计Count

    Description 一 棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. ...

  5. Getopt::Long 模块的简单使用

    用法简介 1.带值参数传入程序内部 ※参数类型:整数, 浮点数, 字串 GetOptions( 'tag=s' => \$tag ); ‘=’表示此参数一定要有参数值, 若改用’:'代替表示参数 ...

  6. in_array 判断问题的疑惑解决。

    面试题中有一条是关于in_array判断的,题目如下: 如何大家没有深入了解in_array的类型判断过程,而是根据经验来选择,肯定很多人也是是选择了D答案的,具体的原因我也是从牛人的博客里面得到答案 ...

  7. 【C语言】字符集和词汇

    C语言字符集和词汇 一.相关基础知识 字符是组成语言的最基本的元素 词汇,又称语汇,是一种语言里所有的(或特定范围的)词和固定短语的总和 二.具体内容 C语言字符集由:字母.数字.空格.标点和特殊字符 ...

  8. HDU 1207

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1207 四柱汉诺塔问题 当 r = (sqrt(8*n+1)-1)/2 时, 存在 count = (n ...

  9. 《C++程序设计》上半部读书笔记

    目录   前言     第一章 C++的初步知识         1 C语言的优点         2 C++产生的背景         3 C++对C的增强         4 如何体会C++的优点 ...

  10. test知识

    内部测试SIT ——system integration testcase 用户测试UAT——user acceptance test SIT是集成测试UAT是验收测试从时间上看,UAT要在SIT后面 ...