以相等的概率生成从1到7的整数

给定一个函数foo()以相等的概率返回从1到5的整数,只使用foo()编写一个函数,以相等的概率返回从1到7的整数。尽量减少对foo()方法的调用次数。此外,不允许使用任何其他库函数,也不允许使用浮点算法。 解决方案: 我们知道foo()返回从1到5的整数。我们如何确保1到7之间的整数以相同的概率出现? 如果我们以相同的概率从1到7的倍数(比如7,14,21,…)生成整数,我们可以使用模除7,然后加1得到从1到7的概率相等的数字。 我们可以使用下面的表达式以相等的概率生成1到21。

null
 5*foo() + foo() -5 

让我们看看如何使用上述表达式。 1.对于第一个foo()的每个值,第二个foo()的值可以有5种可能的组合。因此,总共有25种可能的组合。 2.上述等式返回的值范围为1到25,每个整数正好出现一次。 3.如果方程的值小于22,则返回模除7,然后加1。否则,再次递归调用该方法。因此,返回每个整数的概率变为1/7。 下面的程序显示,表达式只返回1到25之间的每个整数一次。

C++

#include <stdio.h>
int main()
{
int first, second;
for ( first=1; first<=5; ++first )
for ( second=1; second<=5; ++second )
printf ( "%d " , 5*first + second - 5);
return 0;
}


JAVA

// Java code to demonstrate
// expression returns each integer
// from 1 to 25 exactly once
class GFG {
public static void main(String[] args)
{
int first, second;
for ( first= 1 ; first<= 5 ; ++first )
for ( second= 1 ; second<= 5 ; ++second )
System.out.printf ( "%d " , 5 *first + second - 5 );
}
}
// This code is contributed by
// Smitha Dinesh Semwal


Python3

# Python3 code to demonstrate
# expression returns each integer
# from 1 to 25 exactly once
if name = = '__main__' :
for first in range ( 1 , 6 ):
for second in range ( 1 , 6 ):
print ( 5 * first + second - 5 )
# This code is contributed by Smitha Dinesh Semwal.


C#

// C# code to demonstrate expression returns
// each integer from 1 to 25 exactly once
using System;
class GFG {
public static void Main()
{
int first, second;
for ( first = 1; first <= 5; ++first )
for ( second = 1; second <= 5; ++second )
Console.WriteLine ( 5*first + second - 5);
}
}
// This code is contributed by Sam007.


PHP

<?php
// PHP program to Generate integer
// from 1 to 7 with equal probability
$first ;
$second ;
for ( $first = 1; $first <= 5; ++ $first )
for ( $second = 1; $second <= 5; ++ $second )
echo 5 * $first + $second - 5, "" ;
// This code is contributed by ajit.
?>


Javascript

<script>
// Javascript Program to demonstrate
// expression returns each integer
// from 1 to 25 exactly once
// Driver Code
let first, second;
for ( first=1; first<=5; ++first )
for ( second=1; second<=5; ++second ) {
document.write( 5*first + second - 5);
document.write( "<br />" );
}
</script>


输出:

12..2425

时间复杂性: O(1)

辅助空间: O(1)

下面的程序描述了如何使用foo()以相同的概率返回1到7。

C++

// C++ program to Generate integer from
// 1 to 5 with equal probability
#include <stdio.h>
// given method that returns 1 to 5 with equal probability
int foo()
{
// some code here
}
int my_rand() // returns 1 to 7 with equal probability
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
int main()
{
printf ( "%d " , my_rand());
return 0;
}


JAVA

// Java program to Generate integer from
// 1 to 5 with equal probability
class GfG
{
// given method that returns 1 to 5 with equal probability
static int foo()
{
// some code here
return 0 ;
}
// returns 1 to 7 with equal probability
public static int my_rand()
{
int i;
i = 5 *foo() + foo() - 5 ;
if (i < 22 )
return i% 7 + 1 ;
return my_rand();
}
// Driver code
public static void main (String[] args) {
System.out.println(my_rand());
}
}


Python3

# Python3 program to Generate integer
# from 1 to 5 with equal probability
# given method that returns 1 to 5
# with equal probability
def foo():
# some code here
return 0 ;
# returns 1 to 7 with equal probability
def my_rand():
i = 0 ;
i = ( 5 * foo()) + (foo() - 5 );
if (i < 22 ):
if (i < 0 ):
return (i % 7 - 7 ) + 1 ;
else :
return (i % 7 ) + 1 ;
return my_rand();
# Driver code
if __name__ = = '__main__' :
print (my_rand());
# This code contributed by PrinciRaj1992


C#

// C# program to Generate integer from
// 1 to 5 with equal probability
using System;
class GfG
{
// given method that returns 1 to 5 with equal probability
static int foo()
{
// some code here
return 0;
}
// returns 1 to 7 with equal probability
public static int my_rand()
{
int i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
public static void Main () {
Console.Write(my_rand()+ "" );
}
}


PHP

<?php
// PHP program to Generate integer from
// 1 to 5 with equal probability
// given method that returns 1 to 5
// with equal probability
function foo()
{
// some code here
}
// returns 1 to 7 with equal probability
function my_rand()
{
$i ;
$i = 5 * foo() + foo() - 5;
if ( $i < 22)
return $i % 7 + 1;
return my_rand();
}
// Driver code
echo my_rand();
// This code is contributed by Tushil.
?>


Javascript

<script>
// Javascript program to Generate integer from
// 1 to 5 with equal probability
// given method that returns 1 to 5 with equal probability
function foo()
{
// some code here
return 0;
}
// returns 1 to 7 with equal probability
function my_rand()
{
let i;
i = 5*foo() + foo() - 5;
if (i < 22)
return i%7 + 1;
return my_rand();
}
// Driver code
document.write(my_rand());
// This code is contributed by rag2127
</script>


输出:

-4

时间复杂性: O(1)

辅助空间: O(1)

本文由 阿希什·巴恩瓦尔 并由Geeksforgeks团队审核。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享