3

I'm trying to come up with a random number generator which returns a float value between 0 and 1 weights the returned value in either one direction or the other.

Is there any reliable way to do this based on passing in two numbers, say Random(0.5) where '0.5' means the number between 0 and 1 returned will gravitate towards 0.5.

meds
  • 21,699
  • 37
  • 163
  • 314
  • 1
    If it gravitates to something it isn't random anymorw. – PVitt Apr 28 '11 at 10:13
  • 2
    You probably need a lot more information about your problem to find a good solution. You say "gravitate towards". How fast? How much? Are you looking for a normal/gaussian distribution? http://en.wikipedia.org/wiki/Normal_distribution – Lasse V. Karlsen Apr 28 '11 at 10:14
  • no, it would be a non-uniform random number generator – Dan D. Apr 28 '11 at 10:14
  • 10
    @PVitt It can still be random, it's just that the probability distribution isn't uniform. – Lasse V. Karlsen Apr 28 '11 at 10:14
  • If it's 1 or 0, you can get it with `r() = rnd ^ a` and `s() = 1 - rnd ^ a`, for some `a`. For in-between stuff this doesn't make sense, so you probably want a gaussian - which also has plenty to tweak. Just "gravitate" is not saying all that much. – Amadan Apr 28 '11 at 10:20
  • I can see a possible use for this. Somebody say "casino"? – Alex R. Apr 28 '11 at 10:21
  • A typical method for generating a random number with a normal distribution is the Box-Mueller method, unfortunately I can't find a C# implementation of it, and since I've only ever *used* such implementations, I don't understand why my test-implementation returns numbers outside of the range 0..1. – Lasse V. Karlsen Apr 28 '11 at 10:31
  • I've posted my own question related to this: http://stackoverflow.com/questions/5817490/implementing-box-mueller-random-number-generator-in-c – Lasse V. Karlsen Apr 28 '11 at 10:57

2 Answers2

3

What you are referring to is a random number projected onto a bell curve

for which i generally do the following

/// <summary>
/// generate a random number where the likelihood of a large number is greater than the likelihood of a small number
/// </summary>
/// <param name="rnd">the random number generator used to spawn the number</param>
/// <returns>the random number</returns>
public static double InverseBellCurve(Random rnd)
{
    return 1 - BellCurve(rnd);
}
/// <summary>
/// generate a random number where the likelihood of a small number is greater than the likelihood of a Large number
/// </summary>
/// <param name="rnd">the random number generator used to spawn the number</param>
/// <returns>the random number</returns>
public static double BellCurve(Random rnd)
{
    return  Math.Pow(2 * rnd.NextDouble() - 1, 2);
}
/// <summary>
/// generate a random number where the likelihood of a mid range number is greater than the likelihood of a Large or small number
/// </summary>
/// <param name="rnd">the random number generator used to spawn the number</param>
/// <returns>the random number</returns>
public static double HorizontalBellCurve(Random rnd)
{
    //This is not a real bell curve as using the cube of the value but approximates the result set
    return  (Math.Pow(2 * rnd.NextDouble() - 1, 3)/2)+.5;
}

Note you can tweak the formulas to change the shape of the bell to adjust the distribution of the results

for eample a simple Math.Sqrt(rnd.nextdouble()) will slant all numbers towards 1, and a simple Math.Power(rnd.nextdouble(),2) will slant the results towards 0

MikeT
  • 5,398
  • 3
  • 27
  • 43
1

This might help ?

http://www.c-sharpcorner.com/UploadFile/trevormisfeldt/NormalDistribution08302005003434AM/NormalDistribution.aspx

It won't solve your problem precisely, but I'm wondering if modelling a bell curve might provide the tendency that you're looking for.

Interesting problem though, can I ask what you're trying to solve ?

2nd Edit: I just noticed another S/O question that might help:

Random number within a range based on a normal distribution

Community
  • 1
  • 1
Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
  • 1
    I'm working on a game (http://jabberworx.net/meteormania/meteormania.html) which creats meteors of different sizes. I want it to keep generating random meteors but I want more small meteors than big ones but still keep it random.. – meds Apr 28 '11 at 10:45