1

I want to write a random number generation algorithm. But I do not want the numbers to be completely random. I want them to be dependent on a number of different factors. By factors, I mean variables with a value between 0 to 1.

So factors could be -

Bounce         // 0.56 // Should afffect the outcome by 10% // inversely proportional to outcome
Spin           // 0.71 // Should afffect the outcome by 20% // inversely proportional to outcome
Light          // 0.99 // Should afffect the outcome by 5% // directly proportional to outcome
Crowd Support  // 1.00 // Should afffect the outcome by 5% // directly proportional to outcome
Pressure       // 0.89 // Should afffect the outcome by 10% // inversely proportional to outcome
Experience     // 0.76 // Should afffect the outcome by 10% // directly proportional to outcome
Skill          // 0.56 // Should afffect the outcome by 40% // directly proportional to outcome

Now based on these factors, I want to generate a random number between 0-6 or a wicket.

I am writing this algorithm to simulate a cricket game.

What approach could I take to write such a algorithm?

Joe Slater
  • 2,483
  • 6
  • 32
  • 49
  • How does the variables change the outcome? A value of 1 as opposed to a value of 0.5, what does that mean? Higher random results on average? – Lasse V. Karlsen May 19 '13 at 10:38
  • code is irrelevant. Question is pure mathematics. You have different factors, but what are you trying to calculate based on them? What are the relationships between them? – Ilya Ivanov May 19 '13 at 10:38
  • @LasseV.Karlsen Its different for different factors, Higher skill should result in a higher score, but higher pressure should result in a lower score. – Joe Slater May 19 '13 at 10:39
  • @AnkurSharma `Its different for different factors` - why not to write them all in the question at the first place? – Ilya Ivanov May 19 '13 at 10:41
  • @AnkurSharma So then what exactly do you need help with? Since you haven't explained how these factors change the outcome, how do you expect anyone to help here? Your first order of business is detailing how the factors change the outcome. – Lasse V. Karlsen May 19 '13 at 10:41
  • @IlyaIvanov, Edited the question to be more detailed. – Joe Slater May 19 '13 at 10:44
  • @LasseV.Karlsen Edited to show how the factors change the outcome. – Joe Slater May 19 '13 at 10:46
  • @ShadowWizard This is not a duplicate of the chosen question, this is not about a random weighted choice. – Lasse V. Karlsen May 19 '13 at 10:55
  • There is not enough detail in this question about 1) how the values would impact the randomness in such a way that you would get a value of 0-6, or 2) what exactly it is that you want help with, do you want the formula, *a* formula? Do you just want to know how to arrive at the formula? – Lasse V. Karlsen May 19 '13 at 10:57
  • @Lasse "Should afffect the outcome by 10%" sounds like weight to me. Maybe not exact dupe but at least very related. – Shadow The GPT Wizard May 19 '13 at 11:01
  • This article might be helpful as well: http://stackoverflow.com/questions/3519395/probability-of-outcomes-algorithm – jfrankcarr May 19 '13 at 11:02
  • @LasseV.Karlsen I want a function like - randomScore(double skill, double experience, double Bounce ....) and it will return a value between 0 - 6 or a wicket (wicket could be marked by the number -1 may be). So basically I am asking you guys to write me a function since I am not particularly good at mathematics. As for your first question, I will edit the question, just wait. – Joe Slater May 19 '13 at 11:03
  • @ShadowWizard Only insofar it is about probability or randomness, but that would make every question involving randomness a duplicate. – Lasse V. Karlsen May 19 '13 at 11:03
  • 2
    @AnkurSharma The problem is that there is not enough detail, this is a discussion type question since you need to sit down with someone and experiment, discuss the results you get, tinker with the algorithm or the formula, experiment some more, etc. It's not a Q&A question. The formula doesn't exist until after you've worked it out, so you can't just ask someone to give it to you. Nobody has it. – Lasse V. Karlsen May 19 '13 at 11:04
  • 1
    @LasseV.Karlsen Agreed. I need to use my own brain. Thanks for the input. – Joe Slater May 19 '13 at 11:28

1 Answers1

3

Idea is to calculate random coefficient to all factors individually. You will get lower distribution range over resulting characteristic.

int max = 6;

var rand = new Random();

//to make code cleaner, returns 0..1 double
Func<double> next = () => rand.NextDouble(); 

double result = - Bounce       * max * 0.1  * next() 
                - Spin         * max * 0.2  * next() 
                + Light        * max * 0.05 * next()  //0.1
                + CrowdSupport * max * 0.05 * next()  //0.1
                - Pressure     * max * 0.1  * next() 
                + Experience   * max * 0.1  * next()  //0.2
                + Skill        * max * 0.4  * next(); //0.6

//calculated based on weights and negative\positive character
//result would be in range 0 and 6 with 

Another idea is to calculate positive and negative factors separately and them apply random coeficient to all of them, divide by two and add to half of the max. You'll get random distribution from 0 to 6.

double negativeFactor =   Bounce       * max * 0.25   
                        + Spin         * max * 0.5   
                        + Pressure     * max * 0.25;


double positiveFactor =   Light        * max * 0.1
                        + CrowdSupport * max * 0.1
                        + Experience   * max * 0.2
                        + Skill        * max * 0.6;

double result = max / 2 
            + positiveFactor * max * next() / 2
            - negativeFactor * max * next() / 2;

As Lasse V. Karlsen correctly noted, you need to pick weights for positive factors in such a way, so that their sum would be 1. Then distribution will include six a max value, if all negative factors will be zero. Example of such factors I gave in the comments for source code.

For negative factors, you will allow them to decrease resulting value up to 40%. If you want to include 0 as result, you also need to make such coeficient, so that their sum will be 1, examples are also on the comments

Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • How would this ever produce a value of 6 (or close to it)? If all the negative variables are valued at 0, and all the positive values are valued at 1, and all the random values return 1, you still only get up to 3.6. – Lasse V. Karlsen May 19 '13 at 10:52
  • @LasseV.Karlsen Good note. The sum of weights for positive factors should be 1. Corrected my answer. Now if you substitue factors in comments (I do want to leave original OPs weights), you'll get 6 as result – Ilya Ivanov May 19 '13 at 10:57
  • wow. thanks. this is great. I will modify it to fit my needs. Do you know if there is a way to return a wicket (may be a value of -1 or 7). How would I modify this function to do that? – Joe Slater May 19 '13 at 11:19
  • @AnkurSharma what do you mean by wicket? Can you describe it in question including examples? Is it a `2D` array? – Ilya Ivanov May 19 '13 at 11:22
  • Wicket is a part of the sport called cricket (much like baseball) which I am trying to simulate using this algorithm. You can either score runs ranging from 0-6 or you can get caught by a fielder, which is called a wicket. – Joe Slater May 19 '13 at 11:23
  • Never mind, I will think of that myself, you do not need to worry about it. You have already helped me enough. Thanks again. – Joe Slater May 19 '13 at 11:29