0

I am relatively new to Programming and I would like to make a function that can randomly select between 3 things in a game I play! I would like to run this function 100 times and I will choose the item that appears the most number of times. What is the best way to do this in C#?

leppie
  • 115,091
  • 17
  • 196
  • 297
  • 2
    You are going have to show a little effort on your part. Also running a PRNG multiple times will end up with an even distribution. – leppie Jul 15 '14 at 04:30
  • Why run it 100 times and choose the item that appears the most? Just it once to select one of the three items. – J0e3gan Jul 15 '14 at 04:31
  • Note that "best way" questions are usually hard to answer well and consequently discouraged; see the SO guidelines on asking good questions ([ask]); but I have offered one possibility that may help you on your way regardless. – J0e3gan Jul 15 '14 at 06:23

2 Answers2

0

Give options (races) values,

Zerg = 0,
Protoss = 1,
Human = 2

then random a number with the values limit

Create a random number between 0 and 2

No need to run it 100 times, 1 is better.

Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
0

As I commented, you just need to run the random selector once per item choice. Running it 100 times per choice will do nothing but waste time.

You may be able to adapt something like this for your needs:

public sealed class RandomHelper
{
    private static Random Randomizer = new Random();

    private RandomHelper()
    {
        ; // not allowed
    }

    public static string GetRandomRace()
    {
        string[] races = { "Toss", "Terran", "Zerg" };
        var randomVal = Randomizer.Next(0, races.Length);

        return races[randomVal];
    }
}

Usage:

Console.WriteLine(RandomHelper.GetRandomRace());

Four runs of this (wrapped in a console app's Main method for a PoC) yielded:

Toss
Toss
Terran
Zerg
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • 1
    I never said anything about race :P. You starcraft player you. –  Jul 15 '14 at 06:16
  • Okay, it bothered me to be in the dark about the domain: so I searched on "Toss Terran Zerg". :D – J0e3gan Jul 15 '14 at 06:20
  • 1
    so my biggest question comes, How did you arrive at this being the way you programmed? Because, I would like to think I know a little about methods, initializers, objects, and so forth. I just couldn't "Create" this code in my project. I can read it just fine. I just cant Establish this code from scratch –  Jul 15 '14 at 06:23
  • I'll try to avoid a lengthy chat in comments but simply say 1) Practice, practice, practice! 2) I know from experience that instantiating `Random` each time we need `myRandom.Next(int1, int2)` is [not the way to use that type](http://stackoverflow.com/a/4855770/1810429). 3) Reuse of a `Random` instance across random selections and the thought that you may have other game related needs for randomness led me to build a helper class around both ideas (`sealed`, the private default constructor, and static members just being one possible implementation based on this choice). – J0e3gan Jul 15 '14 at 06:37