How to pick item from ArrayList at RANDOM

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

44 Replies - 7232 Views - Last Post: 05 June 2012 - 07:02 PM Rate Topic: -----

#16 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:26 PM

Nope.
Was This Post Helpful? 0
  • +
  • -

#17 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:40 PM

declaring 'new' random number it means more memory usage. Try to keep your memory free. You can DECLARE variable only once, but you can ASSIGN or change value whenever you want.
Was This Post Helpful? 1
  • +
  • -

#18 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:45 PM

Dim rndNum As Integer = rnd.Next(4)

rndNum is an integer value for each random number
Was This Post Helpful? 0
  • +
  • -

#19 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:49 PM

Nope, it's the same value.

What's this part doing? = rnd.Next(4)
Was This Post Helpful? 1
  • +
  • -

#20 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:52 PM

Quote

= rnd.Next(4)
this selects the next element at random until all four element are selected
Was This Post Helpful? 0
  • +
  • -

#21 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:55 PM

Nope. What Object is the Next method on?

This post has been edited by AdamSpeight2008: 27 February 2012 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -

#22 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 04:59 PM

this will generate a random number of value btw 0 and 4
Was This Post Helpful? 0
  • +
  • -

#23 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 05:04 PM

Correct. How many does it produce in your code?
Was This Post Helpful? 0
  • +
  • -

#24 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 05:05 PM

one
Was This Post Helpful? 0
  • +
  • -

#25 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 05:08 PM

Correct. Add how many do you want?

This post has been edited by AdamSpeight2008: 27 February 2012 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#26 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 05:10 PM

four
Was This Post Helpful? 0
  • +
  • -

#27 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 06:08 PM

LoL.. Adam.. a rather interesting way of answering this thread..=D ..Mind i bump in ? hahaha


@timosilver , If you want four times , what should you do ? =P ..but to remind you.. even it is random . it is possible that the generator will generate duplicates .

For example
instead of 0123
it might generate 0112 or 0223 , well the colors might repeat . =) .. just to let you know

This post has been edited by shadachi: 27 February 2012 - 06:26 PM

Was This Post Helpful? 1
  • +
  • -

#28 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 06:12 PM

Check this:
Dim strColor as String
strColor = "Red"
Label1.Text = strColor
strColor = "Green"
Label2.Text = strColor
strColor = "Blue"
Label3.Text = strColor



it can be simpler
Label1.Text = "Red"
Label2.Text = "Green"
Label3.Text = "Blue"



this is my code. Your code starts with:
Dim rndNum as Integer
rndNum = rnd.Next(4)

This post has been edited by sela007: 27 February 2012 - 06:20 PM

Was This Post Helpful? 1
  • +
  • -

#29 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,199
  • Joined: 25-September 09

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 07:30 PM

Guys, I think the point of what Adam was doing was helping the OP work through the problem and understand the solution. We try not to just perform a complete solution without helping the person at least understand the what and why of what they're doing. Not that it is wrong either way it's just an understanding in this community that we don't want to be like all the other sites on the net that just provide a solution, thus, helping the Copy & Paste coders that seem to be so common in the programming world.
Was This Post Helpful? 2
  • +
  • -

#30 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: How to pick item from ArrayList at RANDOM

Posted 27 February 2012 - 07:36 PM

You could try:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim anslist As New ArrayList

        anslist.Add("red")
        anslist.Add("green")
        anslist.Add("blue")
        anslist.Add("yellow")

        Label1.Text = anslist(rndNum(4))
        Label2.Text = anslist(rndNum(4))
        Label3.Text = anslist(rndNum(4))
        Label4.Text = anslist(rndNum(4))

    End Sub

'Generate Random Numbers 


    Public Function rndNum(ByVal high As Integer) As Integer

        Static Random1 As New Random 'So it will give you a new one every time
        Return  Random1.Next(high) 
    End Function

End Class



This post has been edited by ricardosms: 27 February 2012 - 07:40 PM

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3