5 Replies - 5638 Views - Last Post: 18 October 2013 - 05:43 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Challenge: Random Take And Rest.

Posted 15 October 2013 - 08:36 PM

Challenge: Random Take And Rest.

Challenge
Create a generic function that returns a random take of a specified count.
You can choose any item in the source to pick, so long as that item hasn't been picked before.
As well the picked items, return the non-selected items separately.
The input source can not be affected by this function.

This post has been edited by AdamSpeight2008: 15 October 2013 - 08:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Challenge: Random Take And Rest.

#2 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,469
  • Joined: 11-May 12

Re: Challenge: Random Take And Rest.

Posted 16 October 2013 - 05:24 AM

Is the specified count just a list of objects?
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: Random Take And Rest.

Posted 17 October 2013 - 09:11 AM

Clarification. The function will take some source and number, that number is how many items to randomly pick. Think like .Take(5) return the first 5 items, but in this function it returns 5 random (exclusive) items.
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,469
  • Joined: 11-May 12

Re: Challenge: Random Take And Rest.

Posted 17 October 2013 - 09:47 AM

So would this be a correct implementation? It doesn't change the source, it returns the non-selected values as well as the selected values, without selecting randoms of the same value.

    Private Function retRandoms(ByVal source As List(Of Integer), ByVal rndCount As Integer, ByRef lstNonRandoms As List(Of Integer)) As List(Of Integer)
        Dim lstRandoms As New List(Of Integer)
        Dim tmpSource As New List(Of Integer)
        Dim loopCount As Integer = 0
        Dim rndNum As Integer = 0
        Dim rnd As New Random
        If source.Count < rndCount Then
            MessageBox.Show("Number of randoms returned cannot be greater than the number of numbers in the source.")
            Return New List(Of Integer)
        End If
        For i As Integer = 0 To source.Count - 1
            tmpSource.Add(source(i))
        Next
        Do
            loopCount += 1
            rndNum = tmpSource(rnd.Next(0, tmpSource.Count - 1))
            tmpSource.Remove(rndNum)
            lstRandoms.Add(rndNum)
        Loop Until loopCount = rndCount
        For i As Integer = 0 To source.Count - 1
            If Not lstRandoms.Contains(source(i)) Then
                lstNonRandoms.Add(source(i))
            End If
        Next
        Return lstRandoms
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lstSourceInts As New List(Of Integer)
        Dim lstNonRandoms As New List(Of Integer)
        'change the to value to change the number of items in the source
        For i As Integer = 1 To 20
            lstSourceInts.Add(i)
        Next
        ' change the 10 to change the numbe of randoms to return
        Dim lstRandoms As List(Of Integer) = retRandoms(lstSourceInts, 10, lstNonRandoms)
        For Each i As Integer In lstRandoms
            Console.WriteLine("Random: " & i)
        Next
        For Each i As Integer In lstNonRandoms
            Console.WriteLine("Non-Selected: " & i)
        Next
        For Each i As Integer In lstSourceInts
            Console.WriteLine("Source: " & i)
        Next
    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Challenge: Random Take And Rest.

Posted 17 October 2013 - 11:05 AM

Not generic.
Was This Post Helpful? 0
  • +
  • -

#6 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,469
  • Joined: 11-May 12

Re: Challenge: Random Take And Rest.

Posted 18 October 2013 - 05:43 AM

Oh, I'll have to do some research then. I didn't know "generic" had a specific definition. I thought it was just like "some type" of function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1