Picking a random member of a Type List

BlitzMax Forums/BlitzMax Beginners Area/Picking a random member of a Type List

Ryan Burnside(Posted 2006) [#1]
Let me introduce a problem,

I have a tlist that contains all my boss_fragment instances. The name of the tlist is called boss_fragment_list. I need to pick any instance from that list at random and tell it to proform a spawn() function how would this be done?

Thanks in advance.


Grey Alien(Posted 2006) [#2]
you know how big the list is (List.Count) so you can make a random number from 1 to Count. Then loop through the list with For m:MyType = Each In MyList and keep a counter incrementing each time and when the counter is the same as the precalculated random number, use Exit to break out of the list and m is set to the random type.

Hope that makes sense.


Ryan Burnside(Posted 2006) [#3]
Ok thanks, sometimes I tend to overcomplicatte things.


Perturbatio(Posted 2006) [#4]
keep a counter incrementing each time and when the counter is the same as the precalculated random number, use Exit to break out of the list and m is set to the random type.


Or just use ValueAtIndex.


Eric(Posted 2006) [#5]
Agree with Perturbatio

Local P1:TParticle=TParticle(ParticleList.ValueAtIndex(A))



Chris C(Posted 2006) [#6]
ValueatIndex uses a loop too, so bare in mind it will be as slow as the first method

be nice if you could go straight to index A ....


Perturbatio(Posted 2006) [#7]
be nice if you could go straight to index A ....

you could always use an array like this


Ryan Burnside(Posted 2006) [#8]
Well I will be having hundreds of objects checking each other at once so optomizations are very important.


N(Posted 2006) [#9]
Ryan: Use my IList code if you'll be having hundreds of objects. It's much faster if you're sorting or getting an object at a specific index.

Using my code you can do pretty much the same thing as with TLists for getting objects at a specific index:
Local f:Foo = Foo(MyList.ValueAtIndex( Rand( 0, MyList.Count( )-1 ) ))



mindstorms(Posted 2006) [#10]
That looks good, Noel...What type should I use for what sort of problems?


N(Posted 2006) [#11]
I use hash tables for storing data over long periods of time (it pretty much ensures that the data is going to stay in memory since it's going to have a reference until I remove it from the table). Binary trees.. Um, yeah, not sure what you'd use those for, I just wrote it for fun. And you can probably find some use for a linked list where others fail.


mindstorms(Posted 2006) [#12]
Thanks, Noel


Ryan Burnside(Posted 2006) [#13]
Yes, thanks Noel, do you wish to be credited in my game?
P.S. Lego for the win!!


N(Posted 2006) [#14]
No, don't care.


Ryan Burnside(Posted 2006) [#15]
Actually speed isn't important in this case here is my function.

It says Compile Error: Unable to convert from 'boss_fragment' to 'Int'

Function choose_cell()
	Local member%=Rand(1,CountList(boss_fragment.boss_fragment_list))
	Local counter%=0
	For Local i:boss_fragment=EachIn(boss_fragment.boss_fragment_list)
	 counter:+1
		If counter=member
			Return i
			Exit
		End If
	Next
End Function



N(Posted 2006) [#16]
Function choose_cell:boss_fragment()