-2

How do you set percentages well having them look random?

I understand how to make an array random. that's the easy part. Now for the hard part. I would like to know how to set percentages on each item in an array well having it look like each item in that array is being shown in a random order.

can you set percentages for each item in an array? Or do we have only Random to work with?

Kiwilad
  • 85
  • 1
  • 1
  • 12
  • 1
    What about generating a random double in [0, 100] for each item? Would that work for you? – usr Aug 04 '14 at 12:37
  • Why doesn't `Random` work for you? – Patrick Hofman Aug 04 '14 at 12:39
  • 1
    Just to mention that a better alternative to Random (but an overkill in a lot of situations) is http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider(v=vs.110).aspx – Savvas Kleanthous Aug 04 '14 at 12:39
  • _" set percentages on each item in an array"_ You haven't even mentioned the type of the array. What are percentages on a `SomeType[]`? – Tim Schmelter Aug 04 '14 at 12:42
  • could you please give an example? – Kiwilad Aug 04 '14 at 12:44
  • @Kiwilad Do you want to influence how often an item get chosen or have every item once and influence the probability it is the first item? – Manfred Radlwimmer Aug 04 '14 at 12:45
  • Can you show us an example of expected (input and) output? – knittl Aug 04 '14 at 12:50
  • think of a gambling machine... most pictures are random though there is that one picture that's not shown as much as the other pictures. can this be duplicated in the c# language? – Kiwilad Aug 04 '14 at 12:51
  • I want three pictures to be random and have a forth picture shown not as often as the other three – Kiwilad Aug 04 '14 at 12:53
  • Have a look at [picking weighted random numbers](http://stackoverflow.com/q/1761626/112968) – knittl Aug 04 '14 at 12:59
  • @ManfredRadlwimmer if I could influence how often an item will be chosen that will answer my question. – Kiwilad Aug 04 '14 at 13:00
  • @knittl that's kind of what i'm looking for though (to be as honest as possible)really don't understand how to implement it into my code. if you can give a code example then I can give you credit for answering my question – Kiwilad Aug 04 '14 at 13:08
  • @Kiwilad: the answers to the question I linked even contain code samples. – knittl Aug 04 '14 at 13:21

1 Answers1

0

To do this you have to associate a weight to each possibility. It doesn't have to be a percentage.

This would be an example of a generic item with a weight

public class WeightedItem<T>
{
    public T Item { get; set; }
    public int Weight { get; set; }

    public WeightedItem(T item, int weight=1)
    {
        Item = item;
        Weight = weight;
    }
}

To pick a random one of all your items you just give items with a higher weight a better chance

public static class WeightedRandomizer<T>
{
    private static System.Random _random;

    static WeightedRandomizer()
    {
        _random = new System.Random();    
    }

    public static T PickRandom(List<WeightedItem<T>> items)
    {
        int totalWeight = items.Sum(item => item.Weight);
        int randomValue = _random.Next(1, totalWeight);

        int currentWeight = 0;
        foreach (WeightedItem<T> item in items)
        {
            currentWeight += item.Weight;
            if (currentWeight >= randomValue)
                return item.Item;
        }

        return default(T);
    }
}

For example:

var candidates = new List<WeightedItem<string>>
{
    new WeightedItem<string>("Never", 0),
    new WeightedItem<string>("Rarely", 2),
    new WeightedItem<string>("Sometimes", 10),
    new WeightedItem<string>("Very often", 50),
};

for (int i = 0; i < 100; i++)
{
    Debug.WriteLine(WeightedRandomizer<string>.PickRandom(candidates));
}

The chances for these items would be:

"Never" : 0 of 62 times (0%)

"Rarely" : 2 of 62 times (3.2%)

"Sometimes" : 10 of 62 times (16.1%)

"Very often": 50 of 62 times (80.6%)

Instead of strings you can of course use any other type like an image, number or a class of your own.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Thank you so very much Manfred that I can work with. awesome man cheers :-) – Kiwilad Aug 04 '14 at 13:15
  • You are welcome. Some advice for the next time you need help with something like that: Include an example of what you would expect the output to look like, kind of like a blackbox. – Manfred Radlwimmer Aug 04 '14 at 13:18
  • it was a difficult question to ask due to my experience with C#. in future I will give an example of what I am expecting or at the very least my attempt to achieve it. – Kiwilad Aug 04 '14 at 13:23