Hey Guys and Gals,

I'm stuck on a little project im doing myself at home,I need to randomly pick an element from an array and I can only use that element three times,I can randomly pick the element but how do I go about only using it three times,if any one can point me in the right direction please


6 Years
Discussion Span
Last Post by buzzykerbox

It'd help if you told us HOW you need to use that element, but without further information, a simple counter seems like an obvious solution.


You can certainly implement a counting mechanism and try to obtain values and re-obtain a new value if your counter at a given position has already met the limit. Towards the end of the process, you would be need to perform more and more checks because your odds of getting a good value will decrease over time.

Another approach is to perhaps build a dictionary of sorts to hold the values as well as the counts of how many times they have been used. Once they've exceeded the limit, you can remove them from your dictionary so they will no longer be a candidate. Consider this example I quickly cooked up (C# code, sorry)

class RandomizerWithLimits
    public RandomizerWithLimits(HashSet<int> initialValues, int maxUsage)
        ValuesDictionary = new Dictionary<int, int>();
        foreach (int item in initialValues)
            ValuesDictionary.Add(item, 0);

        RandomSelector = new Random();
        MaxUsage = maxUsage;

    private Dictionary<int, int> ValuesDictionary { get; set; }
    private Random RandomSelector { get; set; }
    private int MaxUsage { get; set; }

    public int GetRandomValue()
        int index = RandomSelector.Next(ValuesDictionary.Count);
        KeyValuePair<int, int> pair = ValuesDictionary.ElementAt(index);
        ValuesDictionary[pair.Key] = pair.Value + 1;

        if (ValuesDictionary[pair.Key] == MaxUsage)

        return pair.Key;

    public bool HasElements
        get { return ValuesDictionary.Count > 0; }

And then the class can be used in this fashion

HashSet<int> hash = new HashSet<int>(Enumerable.Range(0, 5));
RandomizerWithLimits randomizer = new RandomizerWithLimits(hash, 3);

while (randomizer.HasElements)

Here, I create a HashSet containing the integers 0 through 4. I use that object to instantiate a randomizer, also passing in the maximum number of uses (3) that I want. All that's left to do is to grab and write values as long as the randomizer still has elements to give.


VB version of code in prior post

Class RandomizerWithLimits

    Public Sub New(ByVal initialValues As HashSet(Of Integer), ByVal maxUsage As Integer)

        Me.ValuesDictionary = New Dictionary(Of Integer, Integer)
        For Each i As Integer In initialValues
            Me.ValuesDictionary.Add(i, 0)

        Me.RandomSelector = New Random()
        Me.MaxUsage = maxUsage

    End Sub

    Dim dictionary As Dictionary(Of Integer, Integer)
    Public Property ValuesDictionary() As Dictionary(Of Integer, Integer)
            Return dictionary
        End Get
        Set(ByVal value As Dictionary(Of Integer, Integer))
            dictionary = value
        End Set
    End Property

    Dim random As Random
    Public Property RandomSelector() As Random
            Return random
        End Get
        Set(ByVal value As Random)
            random = value
        End Set
    End Property

    Dim max As Integer
    Public Property MaxUsage() As Integer
            Return max
        End Get
        Set(ByVal value As Integer)
            max = value
        End Set
    End Property

    Public ReadOnly Property HasElements() As Boolean
            Return ValuesDictionary.Count > 0
        End Get
    End Property

    Public Function GetRandomValue() As Integer

        Dim index As Integer = RandomSelector.Next(ValuesDictionary.Count)
        Dim pair As KeyValuePair(Of Integer, Integer) = ValuesDictionary.ElementAt(index)
        ValuesDictionary(pair.Key) = pair.Value + 1

        If (ValuesDictionary(pair.Key) = MaxUsage) Then
        End If

        Return pair.Key

    End Function

End Class
Module Module1

    Sub Main()

        Dim hash As New HashSet(Of Integer)(Enumerable.Range(0, 5))
        Dim randomizer As New RandomizerWithLimits(hash, 3)

        While (randomizer.HasElements)
        End While


    End Sub

End Module

The program is a modified game of black jack,where the program deals 2 cards to the dealer and the player,i was thinking of doing an array with the value of the cards which are 1 to 11 stored in 1 array and when it randomly picks the elements of the array it stores the indices used in another array,but still having problems???

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.