2

i started studying java about 2 or 3 months ago and i am fairly sucky at algorithms and i started working on a project and right now i need to figure out an algorithm.

Basically, it should go like this:

x (final output) is randomly chosen from 1 to 50. Simple. However, i want it to gradually get harder to get something high depending on variable y. Check the example below to see what i mean.

Example (Final output is x): X = 2 while y is 1. X = 2 while y is 1. X = 5 while y is 2. X = 9 while y is 6. X = 12 while y is 10. X = 14 while y is 15.`

See where i'm getting? It gradually get harder. I'm sure it's not that complicated to do but i can't figure it out.

Thanks.

Nicolas Martel
  • 1,641
  • 3
  • 19
  • 34
  • 3
    Eric Lippert has an [excellent post](http://blogs.msdn.com/b/ericlippert/archive/2012/02/21/generating-random-non-uniform-data-in-c.aspx) on generating non-uniform random data. The code is in C#, but a Java translation would be almost verbatim except for the `UniformDistribution()` method he defines, which would need to be implemented as a Java class that implements `Iterable` or `Iterator`. – Adam Mihalcin Apr 08 '12 at 00:08
  • Basically you have two options: Use some sort of NON-uniform random number generator, or use a UNIFORM random number generator and then scale the data somehow afterwards. Eg, getting a uniform random number between 1 and 2500 and taking its square root will produce numbers between 1 and 50 but biased towards the low end. For your algorithm where you want to slowly apply bias with variable Y you could take the Yth root (after first raising the uniform range to the Yth power). Or there are other similar schemes. (The folks at the Mathematics SO forum have helped me with this sort of thing.) – Hot Licks Apr 08 '12 at 00:17
  • (Or I may have that backwards -- you may want to swap "power" and "root". I'll have to work through an example or 3.) – Hot Licks Apr 08 '12 at 00:20
  • Yeah, I had it backwards. Take a random number between 1 and 707. Square it and divide by 10000. This will give you a random number with a range from 1 to 49.something but biased towards the low end. The initial value 353 (midway in the random range) will give you a result of 12.46 which is only the 25% point of the range from 1 to 50 – Hot Licks Apr 08 '12 at 00:28
  • doesn't match what i'm trying to do well enough. – Nicolas Martel Apr 08 '12 at 02:39
  • Also, in your last comment, you say if the random between 1 and 707 was 353, the result would be 12.46 I think you mean something else that divide because : sqrt(353) / 10000 sure doesn't result in 12.46 Unless i got it all wrong and you meant something else ? – Nicolas Martel Apr 08 '12 at 02:51

2 Answers2

1

in pseudocode:

Return MIN(rand(1,50), rand(1,50);
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
0

This is a good Java example of a non-uniform distribution. Check out this link

Community
  • 1
  • 1
eabraham
  • 4,094
  • 1
  • 23
  • 29