473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to generate geometric random numbers?

Hi all,

I am a newbie to Python and would like to genereate some numbers
according to geometric distribution. However, the Python Random package
seems do not have implemented functionality. I am wondering is there
exist any other libraries that can do this job?

Thanks a lot,

Da

Jul 23 '06 #1
9 9931
On 2006-07-23 17:12:20, My***********@gmail.com wrote:
I am a newbie to Python and would like to genereate some numbers
according to geometric distribution. However, the Python Random package
seems do not have implemented functionality. I am wondering is there
exist any other libraries that can do this job?
The usual way is to generate standard random numbers (linear distribution)
and then apply whatever transformation you need to generate the desired
distribution.

Gerhard

Jul 23 '06 #2
Gerhard Fiedler wrote:
On 2006-07-23 17:12:20, My***********@gmail.com wrote:
>I am a newbie to Python and would like to genereate some numbers
according to geometric distribution. However, the Python Random package
seems do not have implemented functionality. I am wondering is there
exist any other libraries that can do this job?

The usual way is to generate standard random numbers (linear distribution)
and then apply whatever transformation you need to generate the desired
distribution.
That only works if there is such a transformation.

The geometric distribution and many others have been implemented in numpy:

http://www.scipy.org/NumPy

In [1]: from numpy import random

In [2]: random.geometric(0.5, size=100)
Out[2]:
array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
4, 2, 1, 1, 3, 1, 3, 1])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 23 '06 #3
Robert Kern wrote:
Gerhard Fiedler wrote:
On 2006-07-23 17:12:20, My***********@gmail.com wrote:
I am a newbie to Python and would like to genereate some numbers
according to geometric distribution. However, the Python Random package
seems do not have implemented functionality. I am wondering is there
exist any other libraries that can do this job?
The usual way is to generate standard random numbers (linear distribution)
and then apply whatever transformation you need to generate the desired
distribution.

That only works if there is such a transformation.

The geometric distribution and many others have been implemented in numpy:

http://www.scipy.org/NumPy

In [1]: from numpy import random

In [2]: random.geometric(0.5, size=100)
Out[2]:
array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
4, 2, 1, 1, 3, 1, 3, 1])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Thanks a lot. I will try it out.

But I am still surprised because the default Random package in Python
can generate so few discrete random distritbuions, while it can
generate quite a few continuous distribution, including some not very
common one.

Da

Jul 24 '06 #4
My***********@gmail.com writes:
But I am still surprised because the default Random package in Python
can generate so few discrete random distritbuions, while it can
generate quite a few continuous distribution, including some not very
common one.
It looks pretty simple to transform the uniform distribution to the
geometric distribution. The formula for its cdf is pretty simple:

cdf(p,n) = (1-p)**(n-1)*p

For fixed p, if the cdf is c, we get (unless I made an error),

n = log(c, 1-p) - 1

So choose a uniform point c in the unit interval, run it through that
formula, and round up to the nearest integer.

See http://en.wikipedia.org/wiki/Geometric_distribution
for more about the distribution.
Jul 24 '06 #5
Paul Rubin <http://ph****@NOSPAM.invalidwrites:
n = log(c, 1-p) - 1
I meant n = log(c/p, 1-p) - 1
sorry.
Jul 24 '06 #6
Paul Rubin wrote:
Paul Rubin <http://ph****@NOSPAM.invalidwrites:
> n = log(c, 1-p) - 1

I meant n = log(c/p, 1-p) - 1
sorry.
import random
from math import ceil, log

def geometric(p):
""" Geometric distribution per Devroye, Luc. _Non-Uniform Random Variate
Generation_, 1986, p 500. http://cg.scs.carleton.ca/~luc/rnbookindex.html
"""

# p should be in (0.0, 1.0].
if p <= 0.0 or p 1.0:
raise ValueError("p must be in the interval (0.0, 1.0]")
elif p == 1.0:
# If p is exactly 1.0, then the only possible generated value is 1.
# Recognizing this case early means that we can avoid a log(0.0) later.
# The exact floating point comparison should be fine. log(eps) works just
# dandy.
return 1

# random() returns a number in [0, 1). The log() function does not
# like 0.
U = 1.0 - random.random()

# Find the corresponding geometric variate by inverting the uniform variate.
G = int(ceil(log(U) / log(1.0 - p)))
return G

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 24 '06 #7
Robert Kern <ro*********@gmail.comwrites:
G = int(ceil(log(U) / log(1.0 - p)))
I usually owuld write that as int(ceil(log(U, 1.0 - p))).
Jul 24 '06 #8
Paul Rubin wrote:
Robert Kern <ro*********@gmail.comwrites:
> G = int(ceil(log(U) / log(1.0 - p)))

I usually owuld write that as int(ceil(log(U, 1.0 - p))).
Knock yourself out. I was cribbing from my C implementation in numpy.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 24 '06 #9
Robert Kern <ro*********@gmail.comwrites:
I usually owuld write that as int(ceil(log(U, 1.0 - p))).
Knock yourself out. I was cribbing from my C implementation in numpy.
Oh cool, I thought you were pasting from a Python implementation. No prob.
Jul 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
20
by: Levi Campbell | last post by:
Hi, I'm working on a random number generator using the internet as a way to gather entropy, I have two questions. 1. is there a way to capture the internet stream? 2. how would I skip every 2nd,...
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
6
by: Anamika | last post by:
I am doing a project where I want to generate random numbers for say n people.... These numbers should be unique for n people. Two people should not have same numbers.... Also the numbers...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
19
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.