Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generate random numbers following geometric distribution

807605Jun 12 2007 — edited Jun 12 2007
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2007
Added on Jun 12 2007
3 comments
1,124 views