一道腾讯的面试题

程晨c 2011-09-26 11:48:53
加精
已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10。
...全文
16320 516 打赏 收藏 转发到动态 举报
写回复
用AI写文章
516 条回复
切换为时间正序
请发表友善的回复…
发表回复
烟波钓 2012-02-06
  • 打赏
  • 举报
回复

int rand10()
{
return rand7()/2+rand7();
}
Yi_Idea 2012-02-02
  • 打赏
  • 举报
回复
新手试练,代码比较简单~
貌似可以了
static Random rd = new Random();

static void Main(string[] args)
{
int[] result = new int[10];
for (int i = 0; i < 100000; i++)
{
int x = rand10();
result[x-1]++;
}

foreach (int times in result)
{
Console.WriteLine(times);
}

Console.ReadKey();
}

static int rand10()
{
int i = 7, x;
while (i == 7)
{
i = rand7();
}

if (i < 4)
{
x = 0;
}
else
{
x = 5;
}

i = 7;
while (i > 5)
{
i = rand7();
}

return x + i;
}

static int rand7()
{
return rd.Next(1,8);
}

tjwsy 2012-01-30
  • 打赏
  • 举报
回复
static int Rand10() {
return (((Rand7() - 1) * 7 + (Rand7() - 1) * 1) + 1) / 5;
}
  • 打赏
  • 举报
回复
我投降了 呵呵
finkle_zhang 2012-01-13
  • 打赏
  • 举报
回复
错了,
[Quote=引用 530 楼 finkle_zhang 的回复:]

C# code

static int Rand10()
{
double result;
result = Rand7() * 10 / 7;
return (int)Math.Floor(result);
}
[/Quote]
finkle_zhang 2012-01-13
  • 打赏
  • 举报
回复

static int Rand10()
{
double result;
result = Rand7() * 10 / 7;
return (int)Math.Floor(result);
}
pengkangde 2011-11-20
  • 打赏
  • 举报
回复
看哥的最白痴算法
pengkangde 2011-11-20
  • 打赏
  • 举报
回复

int s;
int t;
while (true)
{
t=rand7();
if (t < 4)
{
s = t;
break;
}
if (t == 4)
{
continue;
}
if (t > 4)
{
s=rand7()+3;
break;
}
}
Console.WriteLine(s);
TANK 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 108 楼 phh1989 的回复:]

80L的正解啦
rand10=(rand7-1)/6*9+1
[/Quote]

正解个毛 我运行多次 都是 0 10
li2422121715 2011-11-11
  • 打赏
  • 举报
回复
最后答案是什么
Y_19900106 2011-11-09
  • 打赏
  • 举报
回复
思路:
在构造函数中,定义一个随机数组sum[],该数组中有七个数字,这七个数字是从1~10中随即抽中的七个数字,将已知的七个数字与rand7()中抽取的数字进行比较,如果rand7()中抽取的数字存在于数组sum[]中,则返回该数字,如果不存在,则从{8,9,10}中随机抽取一个数字返回
解析:
rand7()中抽取的每个数字的概率是1/7,而从1~10中抽取的七个数的概率都是1/10,而rand7()中抽取的数字存在于sum[]中的概率是1/7,所以rand10()返回1~7的概率就是1/10,同样,从8~10中随即抽取的一个数字的概率是1/3,而返回7~10中的任意一个数字的概率都是1/10,最后总的来说,从rand10()中返回的1~10中每个数字的概率都编程了1/10
zzz9413 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 518 楼 mysinglelive 的回复:]
引用 517 楼 mysinglelive 的回复:

10是偶数比较容易,那如果是要求素数范围的平均随机数呢?比较11,怎么求?

用rand7来求 rand11 和 rand17
[/Quote]
我在490多楼的时候已经说过了
rand7,先求出rand2
然后套循环求出rand14(rand2=1,返回rand7。rand2=2,返回rand7+7)
rand14求rand11,如果rand14=11以内,返回rand14。如果rand14大于11,继续rand14指导在11以内才返回。
mazekui003 2011-10-11
  • 打赏
  • 举报
回复
给出正确答案:
/// <summary>
/// 已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10。
/// </summary>
public class Class1
{
static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}

/// <summary>
/// 返回1~7随机数
/// </summary>
/// <returns></returns>
public static int Rand7()
{
return new Random(GetRandomSeed()).Next(1, 8);
}
/// <summary>
/// 方案:取10次Rand7()相加,每次值小于4为0,大于4为1,等于4则重取,如果最后值为0,则从头开始再来
/// </summary>
/// <returns></returns>
public static int Rand10()
{
int pos = 0;//当前位置
int times = 10;//循环次数
int total = 0;//总数
while (pos<10)
{
int value = Rand7();
while (value == 4)
{
value = Rand7();
}
//
if (value < 4)
value = 0;
else
value = 1;
//
total += value;
//累计次数
pos++;
//判断是否到了最高次数,如果总数为0,则重置从新开始
if (pos == times && total == 0)
{
pos = 0;
}

}
//
return total;
}
}
jmj49314 2011-10-10
  • 打赏
  • 举报
回复
来学习一下
modyaj 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 80 楼 xingchen_22 的回复:]
忘了是哪个语言里的函数了,就是一个随机函数产生一个0-1之间的自然数,然后生成一个1-10的随机数,大体应该是这样做的
设a的区间(0,1)
a*9的区间(0,9)
(a*9)+1的区间(1,10)
也就是a*9+1
按照上边的方式求
a=rand7()区间为(1,7)那么(a-1)/6的区间为(0,1)
那么(a-1)/6*9的区间为(0,9)
(a-1)/6*9+1的区间为(1……
[/Quote]

个人认为将(0,1)区间上正确的做法用在这里似乎不适合,因为上面的从(0,1)到(1,10)都是连续的 ,但是这里给出的随机函数是散列的点 那么(a-1)/6的点也是散列的 这样的话(a-1)/6*9得到的也只是散列的点 当把区间延伸后必然会使得点的丢失,比如说点 7 我认为就娶不到
公子骏 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 517 楼 mysinglelive 的回复:]

10是偶数比较容易,那如果是要求素数范围的平均随机数呢?比较11,怎么求?
[/Quote]
用rand7来求 rand11 和 rand17
公子骏 2011-10-10
  • 打赏
  • 举报
回复
10是偶数比较容易,那如果是要求素数范围的平均随机数呢?比较11,怎么求?
salecn 2011-10-10
  • 打赏
  • 举报
回复
来学习一下!
copelis 2011-10-10
  • 打赏
  • 举报
回复
二楼的不用乘,直接分条件是不是加5不就完了
woshiwy 2011-10-10
  • 打赏
  • 举报
回复
(rand7()-1)*7+rand7()
然后%10+1
加载更多回复(496)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧