Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Technocrat on Fri 11/12/2009 11:15:53

Title: Making seven random ints in an array different from each other...
Post by: Technocrat on Fri 11/12/2009 11:15:53
Hopefully the topic was expressive enough. What I've got at the moment is an array (int Rainbow[7]), and ten different values that each part of the array can be. To give the first one a randome value is easy enough:

Rainbow[0]=Random(10);

But now I need to make the next one random, but a different value from the one before it, and so on upwards until it does each of the seven parts. Initially, I was thinking of using "while" to go through each one, but it's only seven, so doing the code for each isn't exactly taxing. I'm just stumped as to what I'd tell it to look for each time it went up, or how to make it "re-roll" the random number if it's the same as one of the ones before.

Merci beaucoup!
Title: Re: Making seven random ints in an array different from each other...
Post by: Khris on Fri 11/12/2009 12:08:27
  Rainbow[0] = Random(10);
  int i = 1, k;
  bool again;
  while (i < 7) {
    again = true;
    while (again) {
      Rainbow[i] = Random(10);
      again = false;       // assume it is a new number
      k = 0;
      while (k < i) {
        if (Rainbow[k] == Rainbow[i]) again = true;   // no, it isn't
        k++;
      }
    }
    i++;
  }


That should do it.
Title: Re: Making seven random ints in an array different from each other...
Post by: De-Communizer on Fri 11/12/2009 12:09:26
Something like this?


// Set the first one

 Rainbow[0]=Random(10);

// Make the second one not equal the first
 while (Rainbow[1]==Rainbow[0]){
 Rainbow[1]=Random(10);
 }

// Make the third not equal either of the first two
 while ((Rainbow[2]==Rainbow[0])||(Rainbow[1]==Rainbow[2])){
 Rainbow[2]=Random(10);
 }


And keep kind of repeating the last chunk for each of the seven. It looks like that's the kind of thing you're after?

EDIT: Damn, beaten! Yours looks neater than mine!
Title: Re: Making seven random ints in an array different from each other...
Post by: Wretched on Fri 11/12/2009 14:05:14

int a,b,c,i;
int Rainbow[10]; // Just use 0 - 6 for final values
//create unique values
for (i=0;i<10;i++)
{
Rainbow[i]=i;
}
for (i=0;i<50;i++) //Amount of mixing up
{
//swap two random elements
a=Random(10);
b=Random(10);
c=Rainbow[a];
Rainbow[a]=Rainbow[b];
Rainbow[b]=c;
}
Title: Re: Making seven random ints in an array different from each other...
Post by: paolo on Fri 11/12/2009 19:05:21
With any code that uses Random, you will get the same results if you rerun the program. If you want different numbers each time you run the program you need to seed the random-number generator.

I don't have the code to hand but this can be done using the current time. I'm sure someone else will be able to show you how to do this.
Title: Re: Making seven random ints in an array different from each other...
Post by: Matti on Fri 11/12/2009 19:34:16
I'm pretty sure the standard randomizer already takes the day time into consideration (or something else), because the random numbers are definitely NOT the same everytime you start the program.
Title: Re: Making seven random ints in an array different from each other...
Post by: monkey0506 on Fri 11/12/2009 20:29:24
Quote from: paolo on Fri 11/12/2009 19:05:21If you want different numbers each time you run the program you need to seed the random-number generator.

The AGS Random function is pre-seeded. ;)

Oh and Wretched, your function doesn't seem to fit the initial request. The original post indicates an array size of 7, a maximum value of 10, and each value must be unique. Your code works, but only if the array size and maximum value are the same.

Also it's important to note that AGS's Random is from 0-MAX inclusive so if the array size is 10 then doing Random(10) for an index will produce an invalid index (out of bounds) error. You should use Random(ARRAY_SIZE - 1) if you want a valid 0-based array index. ;)

Further, AGS doesn't have a for loop (yet). The workaround is just simply to use while loops instead. :P
Title: Re: Making seven random ints in an array different from each other...
Post by: Matti on Fri 11/12/2009 20:40:37
Quote from: monkey_05_06 on Fri 11/12/2009 20:29:24
The AGS Random function is pre-seeded. ;)

I meant the Random function too. Do you mean that the numbers resulting of the random function should always be the same when you restart the game?

In my case it's like this:

I have 40 lairs scattered over a map. At every gamestart they are randomly placed, e.g: object[1].x=random(bla),object[1].y=random(bla) etc. At every gamestart they have different positions. But this shouldn't happen if the outcome of the Random function is always the same, right?
Title: Re: Making seven random ints in an array different from each other...
Post by: monkey0506 on Fri 11/12/2009 21:03:52
Sorry Mr. Matti, I was referring to paolo's post. I've now quoted it for clarity. I was reaffirming what you said. Every time you restart the program the Random function will generate a new random. I was just trying to distinguish it from, for example, C++ or php which you must provide some type of seed (usually this is the raw time) which is used in generating the random numbers.
Title: Re: Making seven random ints in an array different from each other...
Post by: Matti on Fri 11/12/2009 21:08:57
Ah, okay, thanks for clarification. I know that problem from Turbo Pascal where you always had to insert randomizer; before using the random function in order to have "real" random numbers. I was happy that this isn't the case with AGS.
Title: Re: Making seven random ints in an array different from each other...
Post by: monkey0506 on Fri 11/12/2009 22:03:52
Well unless you're expecting the same results each time you run the program (which seems a bit silly to call a function named "random" if you expect results in a specific order if you ask me anyway) then having to provide a seed value or call "randomizer" is just frankly a bit redundant. I also am glad Chris had the insight to not require such frivolous measures in AGS. :=
Title: Re: Making seven random ints in an array different from each other...
Post by: Khris on Fri 11/12/2009 23:14:49
Regarding Wretched's code, it's actually pretty elegant and will do the job (after a few minor adjustments).

It's true that Random(10) will return an int from the range of 0 to 10, i.e. one of 11 distinct numbers while int Random[10] does declare only ten variables, but that's no problem at all since a) to assign different random numbers to x variables, one has to use x-1 or greater as the param of the Random function (he did) and b) one is then going to use only the first x variables of the array anyway (as he explained in the code's comment).
Title: Re: Making seven random ints in an array different from each other...
Post by: monkey0506 on Fri 11/12/2009 23:29:38
Quote from: Khris on Fri 11/12/2009 23:14:49It's true that Random(10) will return an int from the range of 0 to 10, i.e. one of 11 distinct numbers while int Random[10] does declare only ten variables, but that's no problem at all since a) to assign different random numbers to x variables, one has to use x-1 or greater as the param of the Random function (he did)

What? He is distinctly setting a to Random(10) and then directly using that as an index in the Rainbow[10] array. Approximately 9.09% of the time this will cause a run-time error. a should instead be set to Random(9) so that it contains a valid index for the Rainbow array (same goes for b).

Besides what he's actually doing isn't "assign[ing] different random numbers to x variables" at all. He's swapping exactly two static values between two randomized indices in an array.

Quote from: Khris on Fri 11/12/2009 23:14:49b) one is then going to use only the first x variables of the array anyway (as he explained in the code's comment).

That's what I get for not reading the commentation just because I already know what the code means and assume I don't have to.  :=
Title: Re: Making seven random ints in an array different from each other...
Post by: TheManInBoots on Sun 02/02/2020 21:15:36
I read through this post and saw that De-communizers idea actually isn't a bad idea at all, if you just add variables and loops. I know I'm too late- umm 11 years?
But I thought it through and it actually gives for a really simple script, so I thought I'd share:

Code (ags) Select
int Rainbow[8],i,n=1; //you need at least 8 arrays
Rainbow[0]=Random(9);
Rainbow[1]=Random(9);
while(n<7)
{
for(i=0;Rainbow[i]!=Rainbow[n]&&i!=n;i  ){}
if(i==n)n  ;
Rainbow[n]=Random(9);
i=0;
}