A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/random/bernoulli_distribution/bernoulli_distribution/ below:

public member function

<random>

std::bernoulli_distribution::(constructor) (1)
explicit bernoulli_distribution (double p = 0.5);
(2)
explicit bernoulli_distribution (const param_type& parm);

Construct bernoulli distribution

Constructs a bernoulli_distribution object with a probability of p (or with the probability specified by object parm).

Parameters
p
Probability of returning true.
This shall be a value between 0.0 and 1.0 (both included).
parm
An object representing the distribution's parameters, obtained by a call to member function param.
param_type is a member type.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// bernoulli_distribution example: fortune-teller
#include <iostream>
#include <string>
#include <random>

int main()
{
  std::cout << "Please, enter a yes/no question (I will answer it):" << std::endl;
  std::string text;
  getline(std::cin,text);

  std::seed_seq seed (text.begin(),text.end());  // seed using question
  std::default_random_engine generator (seed);
  std::bernoulli_distribution distribution(0.5);

  bool result = distribution(generator);
  std::cout << ( result ? "yes" : "no" ) << std::endl;

  return 0;
}

Possible output:
Please, enter a yes/no question (I will answer it):
Should I buy a new laptop?
no


Complexity Constant.

See also
bernoulli_distribution::reset
Reset distribution (public member function)
bernoulli_distribution::param
Distribution parameters (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4