• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

How to pick a random struct instance?

Status
Not open for further replies.

Deleted member 219079

D

Deleted member 219079

Is there any way to pick a random struct instance?

Or is there perhaps a module for it?
 
Level 10
Joined
Jun 6, 2007
Messages
392
Basically structs are integers, so you could just get a random integer. Following example works just fine:

JASS:
struct Data
    integer a
    integer b
endstruct

globals
    integer count
endglobals

function foo takes nothing returns nothing
    local Data dat = GetRandomInt(1, count)
endfunction

I'm not sure if there's a way to get count (number of struct instances) directly from struct, but otherwise you can store it in a global variable.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
It only works this way if you use the standard allocator and do not destroy instances. Otherwise there will be gaps (those of higher id will not be puhed down when you destroy) and count != highest id.

Take a collection type that supports randomization and add the intance to the "all" collection within the constructor/remove it within the destructor.
 
Whenever you create a struct store it into an integer / struct array (either will work). Then increase an integer counter to keep a max count of that integer.
Whenever you destroy it de-index that from your indexed struct array. Then decrease the integer counter.

Pick random number from 1 to the integer counter you used and use that integer / struct you have loaded.
 

Deleted member 219079

D

Deleted member 219079

^can you post example please :/
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
JASS:
globals
    point MapPoint array
endglobals

struct point
    integer x
    integer y
    
    // methods blah blah blah
endstruct


function test takes nothing returns nothing
    local integer i=GetRandomInt(0,100)
    //populate array
    set MapPoint[0x0]=point.create(arguments blah blah blah)
    //...
    set MapPoint[100]=point.create(arguments blah blah blah)
    
    //pick random "point" and tell the player wtv...
    call DisplayTextToPlayer(GetLocalPlayer(),0,0,"Hey, I picked MapPoint["+I2S(i)+"]!!")
    //do something with MapPoint[i]
endfunction
 
Chobibo that will not be a random draw from a collection of structs.

This is what I am talking about.

JASS:
library test

    globals
        private testing array ArrayExample
        private integer MaxInteger
    endglobals

    struct testing extends array
        implement Alloc
        
        static method create takes nothing returns thistype
            // This creates a new array and indexes the array with those.
            local thistype this = thistype.allocate()
            set MaxInteger = MaxInteger + 1
            set ArrayExample[MaxInteger] = this
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            // This loops through to de-index the array from the struct that was ended.
            local integer L = 0
            loop
                exitwhen L > MaxInteger
                if L == this then
                    set ArrayExample[L] = ArrayExample[MaxInteger]
                    set MaxInteger = MaxInteger - 1
                    call this.deallocate()
                    exitwhen true
                endif
                set L = L + 1
            endloop
        endmethod
        
        private static method ReturnRandomStruct takes nothing returns thistype
            return ArrayExample[GetRandomInt(1, MaxInteger)]
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            set t = null
        endmethod
        
    endstruct

endlibrary
 

Deleted member 219079

D

Deleted member 219079

Chobibo that will not be a random draw from a collection of structs.

This is what I am talking about.

JASS:
library test

    globals
        private testing array ArrayExample
        private integer MaxInteger
    endglobals

    struct testing extends array
        implement Alloc
        
        static method create takes nothing returns thistype
            // This creates a new array and indexes the array with those.
            local thistype this = thistype.allocate()
            set MaxInteger = MaxInteger + 1
            set ArrayExample[MaxInteger] = this
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            // This loops through to de-index the array from the struct that was ended.
            local integer L = 0
            loop
                exitwhen L > MaxInteger
                if L == this then
                    set ArrayExample[L] = ArrayExample[MaxInteger]
                    set MaxInteger = MaxInteger - 1
                    call this.deallocate()
                    exitwhen true
                endif
                set L = L + 1
            endloop
        endmethod
        
        private static method ReturnRandomStruct takes nothing returns thistype
            return ArrayExample[GetRandomInt(1, MaxInteger)]
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            set t = null
        endmethod
        
    endstruct

endlibrary
Thanks for the help!

@chobibo your tip helped too ;P

+rep to all
 
Status
Not open for further replies.
Top