Random number distribution

This might be more of a math question.
I need to get a random number that is assumed to be geometric with a mean of 25. I looked into std::geometric_distribution, but that does not look like what I want. std::normal_distribution is not quite right either, or I would have been given a standard deviation. Any ideas on what I can use to get a number that will match the requirement would be a great help.
https://en.wikipedia.org/wiki/Geometric_distribution

The mean of a geometric distribution is (1 - p) / p.


http://en.cppreference.com/w/cpp/numeric/random/geometric_distribution/geometric_distribution

When constructing a std::geometric_distribution you need to know p.


You want the mean to be 25 which gives the following equation to solve.

25 = (1 - p) / p
25p = 1 - p
26p = 1
p = 1 / 26


Now that you know what p should be you can easily construct a std::geometric_distribution that has a mean of 25.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <random>
#include <ctime>

int main()
{
	std::mt19937 rand(std::time(nullptr));
	std::geometric_distribution<> dist( 1.0 / 26.0 );
	
	std::cout << "10 numbers from the geometric distribution with mean 25." << std::endl;
	
	for (int i = 0; i < 10; ++i)
	{
		std::cout << dist(rand) << std::endl;
	}
}
Last edited on
It seems so simple the way you explain it. I was able to figure out the bounds, but that would not work with std::uniform_int_distribution. Thanks!!

Is it any wonder I barely passed statistics.
Topic archived. No new replies allowed.