Populating an array with a random sequence

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
jinxtengu
Posts: 85
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Populating an array with a random sequence

Post by jinxtengu » Wed Nov 25, 2020 4:52 am

Hi, I'm still working on games using Z game editor :D . I've recently upgraded to the newest version, but I am still going to finish my old projects using the versions they were made with originally.

Anyhow, I'm interested in learning how to populate an array with a random sequence of numbers, but importantly I don't want it to repeat any of the same numbers twice.
I was wondering if someone could show me how to write a small piece of code that would perform this function. It would help me alot and I would greatly appreciate it!! :D :mrgreen:

User avatar
Kjell
Posts: 1739
Joined: Sat Feb 23, 2008 11:15 pm

Re: Populating an array with a random sequence

Post by Kjell » Wed Nov 25, 2020 11:30 am

Hi jinxtengu,
jinxtengu wrote:
Wed Nov 25, 2020 4:52 am
I'm interested in learning how to populate an array with a random sequence of numbers, but importantly I don't want it to repeat any of the same numbers twice.
There are quite a few ways to do that, but here's one simple approach:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[//

int[10] numbers;

// Fill array with sequential values

for(int i=0; i<10; i++)
{
  numbers[i] = i;
}

// Shuffle values around

for(int i=0; i<10; i++)
{
  int b = rnd()*10;
  int c = numbers[i];

  numbers[i] = numbers[b];
  numbers[b] = c;
}

// Output values

Print.Text = "";

for(int i=0; i<10; i++)
{
  Print.Text += intToStr(numbers[i]);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnRender>
    <RenderText Name="Print" Text="2857306149"/>
  </OnRender>
</ZApplication>
K

jinxtengu
Posts: 85
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: Populating an array with a random sequence

Post by jinxtengu » Sun Dec 20, 2020 7:43 am

Thank you Kjell. :D

Post Reply