Question random item in list

andrews

Well-known member
Joined
Nov 22, 2011
Messages
132
Programming Experience
5-10
Suppose I have 50 items in a list.
nr (random value)
Can I take item nr = 37 ?
Thanks for any response
 
item in list

Suppose I have 50 items in a list.
nr (random value)
Can I take item nr = 37 ?

the 50 items are 25,36,89,14.....(and on place 37 item = 93) 93,65,47...
can I take immediately item37= 93?
I hope the question is clear now
 
Hi,

Sure you can. As an example, here is a list created with 50 Random Numbers:-

Dim myRandomList As New List(Of Integer)
For randomCount As Integer = 1 To 50
  myRandomList.Add(randomGenerator.Next(101))
Next


What you then need to remember is that every item in the above List now has an Index which represents its Position in that list. In addition to this all indexes in .NET Sequence from Zero, so if you want to grab the random number at Position 37 in the List (being Index No. 36) then you can say:-

MsgBox(myRandomList(36))


Hope that helps.

Cheers.

Ian
 
The fact that these are random values is irrelevant. If you want an item at a specific position in any list or array you simple index it, where the index is zero-based. If you have a list or array with 50 items then they are at indexes 0 to 49. Just provide the index and you get the item back.
VB.NET:
Dim item = myArrayOrCollection(index)
That 'item' variable will then be the type of the items.

By the way, while using a loop is not wrong or onerous, you can do it slightly more succinctly like this:
VB.NET:
Dim list = Enumerable.Range(1, 50).Select(Function(n) myRandom.Next(exclusiveMaximum)).ToArray()
Note that I call ToArray rather than ToList because, unless you intend to add or remove items, an array is more appropriate.
 
item in list

Thanks for your help.
But I knew this method.
But it is not immediately (O(1))
I suppose now that it can not be done.
Regards
 
Why should I assume that you read my mind?

That's exactly the point: you shouldn't. In the first place, you asked us how to do something. When we told you how, you said that you already knew how to do it like that. If you already knew that way and were looking for a different way then you should have told us that in the first place and not simply assume that we would read your mind and know that that was what you wanted. We only know what you tell us so tell us everything that's relevant. If you don;t then you're just wasting our time and that will make us less likely to help you in the future, so you're the one who loses out.
 
random inlist

I was thinking that when I use the word immediately that you should understand that it was not from 1 to 50 but with O(1)
 
I was thinking that when I use the word immediately that you should understand that it was not from 1 to 50 but with O(1)

I would never make that interpretation in a question at the application programming level. Regardless, the suggestion made by IanRyder and myself was to use the List(Of T).Item property and, as the documentation for that property states, both getting and setting that property are O(1) operations. So basically you wasted your own time and ours by asking a question that you already knew the answer to but didn't realise it because you didn't bother to read the documentation. I don't want to discourage people from using sites like this one to get help with issues that they can't solve on their own but I do expect that they will do a reasonable level of research first and that they will then provide a FULL and CLEAR explanation of their issue.
 
Back
Top