This discussion is archived
3 Replies Latest reply: Jun 12, 2007 12:12 PM by 807605 RSS

Generate random numbers following geometric distribution

807605 Newbie
Currently Being Moderated
import java.util.*;
import java.lang.Math.*;

class geometric{

public static void main(String[] args){

Random rand = new Random();
for (int i=0; i<100; i++){
double seed = rand.nextDouble();
double prob=0.2654;
long geo = (long)Math.ceil(Math.log((double)seed)/Math.log(1.0-prob));
System.out.println(geo);}
}
}
I use this code to generate random geometric numbers. The problem is that the resulting numbers don't fit into a certain range. My question is this: how to scale the numbers into a range from 1 to 100 for example or from 0 to 1. A simple solution like finding the bigger number and dividing all the others with it won't work because I need to produce one number at a time and not all 100 at the same time.
I am really desperate so any help will be extremely appreciated.
Thanks in advance.
  • 1. Re: Generate random numbers following geometric distribution
    800282 Newbie
    Currently Being Moderated
    int n = rand.nextInt(100); // n = number in the range [0,99>
  • 2. Re: Generate random numbers following geometric distribution
    807605 Newbie
    Currently Being Moderated
    I use this code to generate random geometric numbers.
    The problem is that the resulting numbers don't fit
    into a certain range.
    Well, would they comply with the geometic distribution if they did?
  • 3. Re: Generate random numbers following geometric distribution
    807605 Newbie
    Currently Being Moderated
    When one looks up "geometric distribution" on Wikipedia one finds something like this:

    ..."For example, suppose an ordinary die is thrown repeatedly until the first time a "1" appears. The probability distribution of the number of times it is thrown is supported on the infinite set { 1, 2, 3, ... } and is a geometric distribution with p = 1/6."...

    The telling thing is the comment "supported on an infinite set" Sometimes you get the one in one roll and sometimes (not very likely) it takes a million.

    You can easily simulate a Bernouli process by just rolling the dice (using the uniform random number generator) and counting how long it takes you to hit the desired number (or range of numbers - for example you would simulate probability 2/3 by rnd.nextInt(3)<2 ). This returns numbers distributed geometrically one at a time, BUT the support set is infinite. What do you mean scaled between 1 and 100? You don't scale infinite sets, and if you chop it off at some point it isn't really geometric now is it?

    Chopping it off is easy. Simply stop counting and stop simulating dice rolls, when you hit the max count that you want to get back.

    And of course you don't need to do a discrete simulation, you can do what you did, using uniformly distributed doubles and log caclulations. BUT the resutt set is infinite as it should be. You can do the same thing there, just mash the infinite tail into the last number, or toss it out completly and generate a new number. In any case, you are introducing an error from a true infinite geometric distribution and replacing it with something that resembles a geometric distribution and you can distribute the error (the places where you decided to cheat) through out the distribution however you like.

    So do whatever you must to achieve the effect that you want but what you say you want to do, to scale a geometric distribution, does not actually make any sense.