java - Multiple Range Random Number Generator -


please me out generate random numbers based on user input %'s.
example, input of 60,10 , 30, output should be:

60% of random numbers of 0-70 range
10% of random numbers of 70-80
30% of random numbers of 80 above range

should use multiple range generator?
or possibilities?

here code:

import java.util.arraylist;  import java.util.list;  import java.util.random;  class randominranges {     private final list<integer> range = new arraylist();      randominranges(int min, int max)     {         this.addrange(min, max);     }      final void addrange(int min, int max)     {         for(int = min; <= max; i++)         {             this.range.add(i);         }     }      int getrandom()     {         return this.range.get(new random().nextint(this.range.size()));     }      public static void main(string[] args)     {         randominranges rir = new randominranges(1, 200);         rir.addrange(50, 60);         system.out.println(rir.getrandom());     } } 

a general approach might follows:

  • based on frequencies, partition unit interval (0, 1) regions.
  • define linear mapping each partition element corresponding desired range.
  • generate random numbers uniformly distributed on (0, 1).
  • based on part of (0, 1) each random number falls, apply appropriate mapping.

using posted example, ranges (0, .6), (.6, .7), (.7, 1). first mapping (0, .6) (0, 70): y = 70 * x / 0.6. second mapping (0.6, 0.7) (70, 80): y = 70 + 100 * (x - 0.6). third mapping require knowing maximum number want generate.

the above works continuous uniform distributions on several ranges. note ranges open (which, continuous distributions isn't issue, since probability of hitting boundary point 0 , ties can resolved arbitrarily. if want integer values, can use same approach , take floor of result. bit of care needs taken ensure endpoints treated avoid introducing bias.

finally (hopefully it's obvious): work if desired output ranges disjoint. discrete distributions contiguous ranges (as in example), require deciding range each boundary point falls.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -