How to implement a random number generator that is not random?

I need a random number generator that generates a different number between n and m, but not with equal probability. I want to set the value of x between n and m, where the probability is the highest:

enter image description here

Is there an easy way to do this using the Random class? The probability should be in the form of a binomial distribution or something similar (it doesn’t matter that its exact binomial distribution, rough approximations are also good)

EDIT

Perhaps I need to clarify: I'm not looking for a binomial or Gaussian distribution, and also something like this: enter image description here

I want to determine the value of x where the highest value should be.

EDIT

Unfortunately, the previously accepted answer does not seem to work as I suspected. So I'm still looking for an answer!

+6
c # algorithm random
source share
4 answers

You can use Box-Muller to generate a sequence of pseudorandom normally distributed numbers from a sequence of numbers evenly distributed between 0 and 1.

Box-muller transform

+6
source share

The Java SDK has a good implementation of Random.nextGaussian (taken from http://download.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#nextGaussian ())

Hope it's pretty clear how to deal with java source in C #

synchronized public double nextGaussian() { if (haveNextNextGaussian) { haveNextNextGaussian = false; return nextNextGaussian; } else { double v1, v2, s; do { v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); double multiplier = Math.sqrt(-2 * Math.log(s)/s); nextNextGaussian = v2 * multiplier; haveNextNextGaussian = true; return v1 * multiplier; } } 

UPDATE: How I did a median shift:

 public static float gaussianInRange(float from, float mean, float to) { if( !(from < mean && mean < to) ) throw new IllegalArgumentException(MessageFormat.format("RandomRange.gaussianInRange({0}, {1}, {2})", from, mean, to)); int p = _staticRndGen.nextInt(100); float retval; if (p < (mean*Math.abs(from - to))) { double interval1 = (_staticRndGen.nextGaussian() * (mean - from)); retval = from + (float) (interval1); } else { double interval2 = (_staticRndGen.nextGaussian() * (to - mean)); retval = mean + (float) (interval2); } while (retval < from || retval > to) { if (retval < from) retval = (from - retval) + from; if (retval > to) retval = to - (retval - to); } return retval; } 
+5
source share

You need a generator working on the "Normal Distribution". Have a look here: http://www.csharpcity.com/reusable-code/random-number-generators/

+1
source share

smth is relatively simple. You can generate 2 random numbers: 1st determines how close to x the second random number will be.

You can use any breakpoints / functions that you like.

0
source share

All Articles