zenefits oa - random(5) to generate a random(7)

If given a function that generates a random number from 1 to 5, how do you use this function to generate a random number from 1 to 7 with the same probability? (ie. function a has probability 1/5 per number, function b would have probability 1/7).

int sum = 0;
int times = 5;
while(times>0){
  times--;
  sum += rand(5);
}

// 5 ~ 25  = 3 *7
return (sum - 2) / 3;

时间: 2024-08-12 19:33:53

zenefits oa - random(5) to generate a random(7)的相关文章

How to generate a random number in R

Generate a random number between 5.0 and 7.5x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数x2 <- runif(10, 5.0, 7.5)# 参数10表示产生10个随机数 Generate a random integer between 1 and 10x3 <- sample(1:10, 1)  # 参数1表示产生一个随机数x4 <- sample(1:10, 5, replace=T) # 参数5表

LeetCode 528. Random Pick with Weight / 497. Random Point in Non-overlapping Rectangles

528. Random Pick with Weight 根据weight随机选取一个数,用 Prefix Sum+Binary Search 来解决. https://www.geeksforgeeks.org/random-number-generator-in-arbitrary-probability-distribution-fashion/ class Solution { public: vector<int> prefixSum; int total=0; Solution(v

zenefits oa - sort integer array in lexographical order

[ 12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000 ] Then the output should be: [ 1 | 100000 | 12 | 222 | 23 | 2434 | 56 | 654 ] 建立自己的comparator: Comparator<Integer> intLexCompare = new Comparator<Integer>() { int compareTo( Integer x, Integer y)

Java8初体验(二)Stream语法详解

原文链接:http://ifeve.com/stream/ 1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel aggregate operations. 我们来解读一下上面的那句话: Stream是元素的集合,这点让Stream看起来用些类似Iterator: 可以支持顺序和并行的对原Stream进行汇聚的操作: 大家可以把Stream当成一个高级版本的

按字典的值对字典进行排序

有时有一种需要,求要按字典的值对字典值进行排序: 下面有两种方法 #!/usr/bin/env python #coding:utf-8 #@Author:Andy from random import randint # generate a random dict dict1 = {x :randint(1, 100) for x in "abczyx"} print(dict1) #method 1 # use built in function sorted ,zip dict

laravel小抄

原文地址:http://cheats.jesse-obrien.ca/ Artisan // Displays help for a given command php artisan --help OR -h // Do not output any message php artisan --quiet OR -q // Display this application version php artisan --version OR -V // Do not ask any interac

机器学习-CrossValidation交叉验证详解

版权声明:本文为原创文章,转载请注明来源. 1.原理 1.1 概念 交叉验证(Cross-validation)主要用于模型训练或建模应用中,如分类预测.PCR.PLS回归建模等.在给定的样本空间中,拿出大部分样本作为训练集来训练模型,剩余的小部分样本使用刚建立的模型进行预测,并求这小部分样本的预测误差或者预测精度,同时记录它们的加和平均值.这个过程迭代K次,即K折交叉.其中,把每个样本的预测误差平方加和,称为PRESS(predicted Error Sum of Squares). 1.2

使用Javascript实现随机字符串

方法一(其实是毫秒时间数字字符串): Javascript代码   function randomString() { return '' + new Date().getTime(); } 方法二(随机字母数字字符串): Javascript代码   var alpha_num_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split(''); function randomString(leng

【转】Java8初体验(二)Stream语法详解

原文链接 http://ifeve.com/stream/ Java8初体验(二)Stream语法详解 感谢同事[天锦]的投稿.投稿请联系 [email protected]上篇文章Java8初体验(一)lambda表达式语法比较详细的介绍了lambda表达式的方方面面,细心的读者会发现那篇文章的例子中有很多Stream的例子.这些Stream的例子可能让你产生疑惑,本文将会详细讲解Stream的使用方法(不会涉及Stream的原理,因为这个系列的文章还是一个快速学习如何使用的). 1. Str