473,665 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9950
On 2006-07-23 17:12:20, My***********@g mail.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***********@g mail.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.geometri c(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***********@g mail.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.geometri c(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***********@g mail.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.i nvalidwrites:
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.i nvalidwrites:
> 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*********@gm ail.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*********@gm ail.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*********@gm ail.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
8284
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 B
20
2159
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, 3rd, or 4th byte to protect privacy?
14
9877
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 to 1 . I am not sure how to do this. Any idea? Thanks.
12
5219
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
6
2771
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 should not be repeted.. Do anyone have some nice algorithm for that? Or anyone can suggest me any type of book or site for that purpose?
20
7843
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 numbers, multiply them, and ask you for the result. It also needs to have four responses for both right and wrong answers and should print them randomly as well. The program should use at least 2 functions. Any help would be greatly appreciated. ...
9
6598
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
7207
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 uniformly distributed and unique numbers ?
19
13664
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
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8779
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.