由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - random numbers
相关主题
问个随机数的问题求教Careercup 150 上的一道题目
推荐一个random generation的总结如果给随即函数rand[1,5] 如何产生rand[1,7]
这个题没怎么看大家讲过Randomly shuffle decks of cards
请教个弱题:random generator: from 1~5 to 1~7how to shuffle a deck of cards?
Epic 店面被考到coding的题目。。。(面经)谁还记得这道面试题吗?
[合集] 那个Google random generate 1-7的题怎么做啊?发个google的面试题
问一个面试题一个set,有add(i), del(i), count(i), size。写random()。
问一道题晒一道有意思的面试题
相关话题的讨论汇总
话题: random话题: generate话题: numbers话题: rand6
进入JobHunting版参与讨论
1 (共1页)
c***2
发帖数: 838
1
where are the basics/algorithms to solve random number problems?
for example,
you have a dice to generate random numbers 1-6
how to generate 1-5 or 1-8?
Thanks,
f*****w
发帖数: 2602
2
同问
s******o
发帖数: 2233
3
suppose r() genereates 1-6
then (r()-1)/5*4+1 generates 1-5

【在 c***2 的大作中提到】
: where are the basics/algorithms to solve random number problems?
: for example,
: you have a dice to generate random numbers 1-6
: how to generate 1-5 or 1-8?
: Thanks,

c***2
发帖数: 838
4
is it okay doing this to get 1-5:
do {
num=r();
} while (num==6);
return num;
g*********s
发帖数: 1782
5
yes.

【在 c***2 的大作中提到】
: is it okay doing this to get 1-5:
: do {
: num=r();
: } while (num==6);
: return num;

P********l
发帖数: 452
6
http://www.sureinterview.com/shwqst/125002
http://www.sureinterview.com/schlsc?q=random
从rand6到rand8很简单。因为rand8的因子可以从rand6得到.
rand8 = (((rand6-1)*6 + (rand6-1))*6 + (rand6-1)) % 8 + 1.
This is an old question.

【在 c***2 的大作中提到】
: where are the basics/algorithms to solve random number problems?
: for example,
: you have a dice to generate random numbers 1-6
: how to generate 1-5 or 1-8?
: Thanks,

g*********s
发帖数: 1782
7
或者生成1~4,再生成1~2,相乘。

【在 P********l 的大作中提到】
: http://www.sureinterview.com/shwqst/125002
: http://www.sureinterview.com/schlsc?q=random
: 从rand6到rand8很简单。因为rand8的因子可以从rand6得到.
: rand8 = (((rand6-1)*6 + (rand6-1))*6 + (rand6-1)) % 8 + 1.
: This is an old question.

p******r
发帖数: 2999
8
how do you get 5?

【在 g*********s 的大作中提到】
: 或者生成1~4,再生成1~2,相乘。
x*****p
发帖数: 1707
9
The basic idea is like this.
If n>m and gcd(n,m) = 1, from rand_n to generate rand_m, we just do rand_n
and skip those who is greater than m. Then 1, ..., m is evenly distributed,
but the total probability is less than 1.
If n numbers, let k be the greatest int such that km greater than km and it is done.
l*****a
发帖数: 14598
10
you can't do like this
there are 1/6 probability to get one of 1,2,3,4,5,6
then also 1/6 for r()-1 as 0,1,2,3,4,5
so the probability of each of below is also 1/6
0/5*4+1=1
1/5*4+1
2/5*4+1
3/5*4+1
4/5*4+1
5/5*4+1=5
BTW,for the middle four,i assume that they are all 1 for integer.
since 1/5 should be 0.

【在 s******o 的大作中提到】
: suppose r() genereates 1-6
: then (r()-1)/5*4+1 generates 1-5

l*****a
发帖数: 14598
11
my solution to this is
while(1)
{
sum=(random6()-1)*6+(random6()-1);//sum is 0..35
if(sum==35) continue;
else return sum%5;
}

【在 l*****a 的大作中提到】
: you can't do like this
: there are 1/6 probability to get one of 1,2,3,4,5,6
: then also 1/6 for r()-1 as 0,1,2,3,4,5
: so the probability of each of below is also 1/6
: 0/5*4+1=1
: 1/5*4+1
: 2/5*4+1
: 3/5*4+1
: 4/5*4+1
: 5/5*4+1=5

f*****w
发帖数: 2602
12

你这个0..35不是uniform的

【在 l*****a 的大作中提到】
: my solution to this is
: while(1)
: {
: sum=(random6()-1)*6+(random6()-1);//sum is 0..35
: if(sum==35) continue;
: else return sum%5;
: }

a******e
发帖数: 95
13
Think one more time and you'll know it is.
Hint: it uses base 6 representation.

【在 f*****w 的大作中提到】
:
: 你这个0..35不是uniform的

1 (共1页)
进入JobHunting版参与讨论
相关主题
晒一道有意思的面试题Epic 店面被考到coding的题目。。。(面经)
请教一道面试题[合集] 那个Google random generate 1-7的题怎么做啊?
讨论一道经典题问一个面试题
random(5) generate random(7)问一道题
问个随机数的问题求教Careercup 150 上的一道题目
推荐一个random generation的总结如果给随即函数rand[1,5] 如何产生rand[1,7]
这个题没怎么看大家讲过Randomly shuffle decks of cards
请教个弱题:random generator: from 1~5 to 1~7how to shuffle a deck of cards?
相关话题的讨论汇总
话题: random话题: generate话题: numbers话题: rand6