• 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.

[Solved] Random numbers array

Status
Not open for further replies.
Level 4
Joined
Dec 25, 2012
Messages
66
Here is the simplest algorythm ever to fill an array with random numbers:

  • Set tmpI = 16
  • For each (Integer tmpI) from 0 to 15, do (Actions)
    • Set array[tmpI] = (Random integer number between 1 and 10)
    • Set tmpI = (tmpI - 1)
It works, however, it fills the array with 15 similar numbers. For example, sixteen 8's or 12's.

88888888888888888888888
121212121212121212121212

How to make it work properly? Suggestions are appreciated. Thanx!
 
Level 21
Joined
Mar 27, 2012
Messages
3,232
Here is the simplest algorythm ever to fill an array with random numbers:

  • Set tmpI = 16
  • For each (Integer tmpI) from 0 to 15, do (Actions)
    • Set array[tmpI] = (Random integer number between 1 and 10)
    • Set tmpI = (tmpI - 1)
It works, however, it fills the array with 15 similar numbers. For example, sixteen 8's or 12's.

88888888888888888888888
121212121212121212121212

How to make it work properly? Suggestions are appreciated. Thanx!

I don't get it. Why are you always setting the same index?

For each (Integer tmpI) from 0 to 15, do (Actions)
Increases tmpI by 1 every iteration
Set tmpI = (tmpI - 1)
Decreases by 1 every iteration

I'm surprised that it even works.
 
Level 4
Joined
Dec 25, 2012
Messages
66
This is the full trigger as it is ingame:

  • Events
    • Map initialization
  • Conditions
  • Acions
  • Set tmpI = 16
  • For each (Integer tmpI) from 0 to 15, do (Actions)
    • Cycle - actions
      • Set array[tmpI] = (Random integer number between 1 and 16)
      • Set tmpI = (tmpI - 1)
And sorry about the 12's. It should have been from 0 to 15.
 
Level 4
Joined
Dec 25, 2012
Messages
66
I don't get it. Why are you always setting the same index?

For each (Integer tmpI) from 0 to 15, do (Actions)
Increases tmpI by 1 every iteration
Set tmpI = (tmpI - 1)
Decreases by 1 every iteration

I'm surprised that it even works.

If what you're sayng is right, I must've misunderstood the way worldeditor's loops work. But where is the function in my trigger that increases tmpI?
That's how I would have it done in other syntax:

for(i = 16, i<=1, i-=1);
{array[tmpI] = rnd(1,16);
}

I'll try your suggestion though. now it looks like this:

  • Events
    • Map initialization
  • Conditions
  • Acions
  • For each (Integer tmpI) from 0 to 15, do (Actions)
    • Cycle - actions
      • Set array[tmpI] = (Random integer number between 1 and 16)
Still the same values are given.
 
Level 21
Joined
Mar 27, 2012
Messages
3,232
If what you're sayng is right, I must've misunderstood the way worldeditor's loops work. But where is the function in my trigger that increases tmpI?
That's how I would have it done in other syntax:

for(i = 16, i<=1, i-=1);
{array[tmpI] = rnd(1,16);
}

I'll try your suggestion though. now it looks like this:

  • Events
    • Map initialization
  • Conditions
  • Acions
  • For each (Integer tmpI) from 0 to 15, do (Actions)
    • Cycle - actions
      • Set array[tmpI] = (Random integer number between 1 and 16)
Still the same values are given.

Well you can turn it to custom text to see what it contains. If I remember correctly, then it automatically increments in GUI.

Then again the problem might also be that loops don't change the random seed, but I'm not sure.
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
To construct random numbers in an array, you should have a list of numbers first in an array too.

We should have 2 sets, Set A is the list of numbers, Set B is the numbers of random.

From the way I see it, you are trying to get a set of random numbers within 1 ~ 10, with 15 elements in that array without repetition, right ?
You see, you want from something that is not sufficient to uphold your needs, therefore you need to either change the range of random numbers or the amount of element of random numbers.

It should always be: Element <= Range (Fix that in comment section, it should <=, not <).
For example: 15 : 20

You will have a set containing 15 elements to choose from 20 ranges of number - this is logic.
You CANNOT in any way, have 20 elements to choose from 15 elements without repetition.

Here's a test map, press ESC key to test it how many times you want.
 

Attachments

  • Constructing Random Set.w3x
    16.5 KB · Views: 92
Level 4
Joined
Dec 25, 2012
Messages
66
JASS:
function Trig_CreateDeckofDoors_Actions takes nothing returns nothing
    set udg_tmpI = 0
    loop
        exitwhen udg_tmpI > 15
        set udg_array[udg_tmpI] = GetRandomInt(1, 16)
        set udg_tmpI = udg_tmpI + 1
    endloop
endfunction

//===========================================================================
function InitTrig_CreateDeckofDoors takes nothing returns nothing
    set gg_trg_CreateDeckofDoors = CreateTrigger(  )
    call TriggerAddAction( gg_trg_CreateDeckofDoors, function Trig_CreateDeckofDoors_Actions )
endfunction

Custom text.
 
Level 4
Joined
Dec 25, 2012
Messages
66
To construct random numbers in an array, you should have a list of numbers first in an array too.

We should have 2 sets, Set A is the list of numbers, Set B is the numbers of random.

From the way I see it, you are trying to get a set of random numbers within 1 ~ 10, with 15 elements in that array without repetition, right ?
You see, you want from something that is not sufficient to uphold your needs, therefore you need to either change the range of random numbers or the amount of element of random numbers.

It should always be: Element <= Range (Fix that in comment section, it should <=, not <).
For example: 15 : 20

You will have a set containing 15 elements to choose from 20 ranges of number - this is logic.
You CANNOT in any way, have 20 elements to choose from 15 elements without repetition.

Here's a test map, press ESC key to test it how many times you want.

Worked like a charm. All kudos to you.
 
Status
Not open for further replies.
Top