📜  以25%和75%的概率产生0和1

📅  最后修改于: 2021-05-25 06:10:30             🧑  作者: Mango

给定一个函数rand50()以相等的概率返回0或1,仅使用rand50()编写一个函数以7%的概率返回1并以25%的概率返回0的函数。最小化对rand50()方法的调用次数。同样,不允许使用任何其他库函数和浮点运算。

这个想法是使用按位或。按位“或”取两个位,如果两个位都为0,则返回0,否则结果为1。因此,它有75%的概率返回1。

下面是上述想法的实现:

C++
// Program to print 1 with 75% probability and 0
// with 25% probability
#include 
using namespace std;
  
// Random Function to that returns 0 or 1 with
// equal probability
int rand50()
{
    // rand() function will generate odd or even
    // number with equal probability. If rand()
    // generates odd number, the function will
    // return 1 else it will return 0.
    return rand() & 1;
}
  
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// Bitwise OR
bool rand75()
{
    return rand50() | rand50();
}
  
// Driver code to test above functions
int main()
{
    // Initialize random number generator
    srand(time(NULL));
  
    for(int i = 0; i < 50; i++)
        cout << rand75();
  
    return 0;
}


Java
// Java program to print 1 with 75% probability and 0 
// with 25% probability 
class GFG 
{
  
    // Random Function to that returns 0 or 1 with 
    // equal probability 
    static int rand50()
    {
        // rand() function will generate odd or even 
        // number with equal probability. If rand() 
        // generates odd number, the function will 
        // return 1 else it will return 0. 
        return (int) (10 * Math.random()) & 1;
    }
  
    // Random Function to that returns 1 with 75% 
    // probability and 0 with 25% probability using 
    // Bitwise OR 
    static int rand75()
    {
        return rand50() | rand50();
    }
  
    // Driver code 
    public static void main(String[] args)
    {
        // Initialize random number generator 
        //srand(time(null)); 
  
        for (int i = 0; i < 50; i++)
        {
            System.out.print(rand75());
        }
  
    }
  
}
  
// This code is contributed by 29AjayKumar


PHP


C++
// Program to print 1 with 75% probability and 0
// with 25% probability
#include 
using namespace std;
  
// Random Function to that returns 0 or 1 with
// equal probability
int rand50()
{
    // rand() function will generate odd or even
    // number with equal probability. If rand()
    // generates odd number, the function will
    // return 1 else it will return 0.
    return rand() & 1;
}
  
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// left shift and Bitwise XOR
int rand75()
{
    // x is one of {0, 1}
    int x = rand50();
  
    x = x << 1;
  
    // x is now one of {00, 10}
  
    x = x ^ rand50();
  
    // x is now one of {00, 01, 10, 11}
  
    return (x > 0) ? 1 : 0;
}
  
// Driver code to test above functions
int main()
{
    // Initialize random number generator
    srand(time(NULL));
  
    for (int i = 0; i < 50; i++)
        cout << rand75();
  
    return 0;
}


Java
// Java program to print 1 with 75% probability and 0
// with 25% probability
class GFG
{
  
// Random Function to that returns 0 or 1 with
// equal probability
static int rand50()
{
    // rand() function will generate odd or even
    // number with equal probability. If rand()
    // generates odd number, the function will
    // return 1 else it will return 0.
    return (int) (10 * Math.random()) & 1;
}
  
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// left shift and Bitwise XOR
static int rand75()
{
    // x is one of {0, 1}
    int x = rand50();
  
    x = x << 1;
  
    // x is now one of {00, 10}
  
    x = x ^ rand50();
  
    // x is now one of {00, 01, 10, 11}
  
    return (x > 0) ? 1 : 0;
}
  
// Driver code
public static void main(String[] args)
{
  
    for (int i = 0; i < 50; i++)
        System.out.print(rand75());
}
}
  
// This code is contributed by 29AjayKumar


PHP
 0) ? 1 : 0;
}
  
// Driver code 
  
// Initialize random
// number generator
srand(time(NULL));
  
for ($i = 0; $i < 50; $i++)
    echo rand75();
      
// This code is contributed 
// by ajit
?>


输出:

11101111110010010110011111111101111110111100011000

在相似的行上,我们还可以使用按位AND 。由于它以75%的概率返回0,因此我们必须将结果取反。

// Random Function to that returns 1 with 75% 
// probability and 0 with 25% probability using
// Bitwise AND
bool rand75() 
{
    return !(rand50() & rand50());
}

我们也可以将OR和AND运算符替换为Bitwise OR和Bitwise AND运算运算符

// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using 
// OR or AND operator
int rand75()
{
    return !(rand50() && rand50());
    // return rand50() || rand50()
}

我们还可以使用左移运算符和按位XOR来获得结果–

C++

// Program to print 1 with 75% probability and 0
// with 25% probability
#include 
using namespace std;
  
// Random Function to that returns 0 or 1 with
// equal probability
int rand50()
{
    // rand() function will generate odd or even
    // number with equal probability. If rand()
    // generates odd number, the function will
    // return 1 else it will return 0.
    return rand() & 1;
}
  
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// left shift and Bitwise XOR
int rand75()
{
    // x is one of {0, 1}
    int x = rand50();
  
    x = x << 1;
  
    // x is now one of {00, 10}
  
    x = x ^ rand50();
  
    // x is now one of {00, 01, 10, 11}
  
    return (x > 0) ? 1 : 0;
}
  
// Driver code to test above functions
int main()
{
    // Initialize random number generator
    srand(time(NULL));
  
    for (int i = 0; i < 50; i++)
        cout << rand75();
  
    return 0;
}

Java

// Java program to print 1 with 75% probability and 0
// with 25% probability
class GFG
{
  
// Random Function to that returns 0 or 1 with
// equal probability
static int rand50()
{
    // rand() function will generate odd or even
    // number with equal probability. If rand()
    // generates odd number, the function will
    // return 1 else it will return 0.
    return (int) (10 * Math.random()) & 1;
}
  
// Random Function to that returns 1 with 75%
// probability and 0 with 25% probability using
// left shift and Bitwise XOR
static int rand75()
{
    // x is one of {0, 1}
    int x = rand50();
  
    x = x << 1;
  
    // x is now one of {00, 10}
  
    x = x ^ rand50();
  
    // x is now one of {00, 01, 10, 11}
  
    return (x > 0) ? 1 : 0;
}
  
// Driver code
public static void main(String[] args)
{
  
    for (int i = 0; i < 50; i++)
        System.out.print(rand75());
}
}
  
// This code is contributed by 29AjayKumar

的PHP

 0) ? 1 : 0;
}
  
// Driver code 
  
// Initialize random
// number generator
srand(time(NULL));
  
for ($i = 0; $i < 50; $i++)
    echo rand75();
      
// This code is contributed 
// by ajit
?>

输出:

01101110111011000111111111110001111011101110110110

请注意,每次运行这些解决方案时,它们都会产生不同的结果