Random method without repeating
#1

Well,i'm working on a easter egg system with random coordinates,at this point,it just fine.

But,i'm worried about the random function,how can i use it without repeat any of the coordinates ?
My last try was that,from other system with other ends:

PHP код:
new Float:EasterEggs[][]=
{
    {
XYZ},
    {
XYZ},
    {
XYZ},
    
//etc..
};
new 
cCheck[sizeof(EasterEggs)];
public 
OnFilterScriptInit()
{
    new 
randomCoordSelectCoord;
    do{
        for(new 
0sizeof(cCheck);i++)
        {
            if(
cCheck[i] != 0SelectCoord++;
            if(
SelectCoord == sizeof(cCheck[i]))
            {
                for(new 
0sizeof(cCheck); i++)
                {
                     
cCheck[i] = 0;
                     
randomCoord random(sizeof(EasterEggs));
                }while(
cCheck[randomCoord])
                {
                    
CreateEE(EasterEggs[randomCoord][0],EasterEggs[randomCoord][1],EasterEggs[randomCoord][2]);
                }
            }
         }
      }

So,what you think guys ?Did you know some method to not repeat with the random function ?
Reply
#2

Код:
new Float:EasterEggs[][]= 
{ 
    {X, Y, Z}, 
    {X, Y, Z}, 
    {X, Y, Z}, 
    //etc.. 
}; 
new lastcoords=-1;
stock SpawnEasterEgg()
{
new randomCoord;
randomCoord = random(sizeof(EasterEggs));
while(randomCoord==lastcoords)
{
      randomCoord = random(sizeof(EasterEggs));
}
lastcoords = randomCoord;
// spawn it.
}
Edit : Are you going to spawn all of the easter eggs at one time or you will spawn them after a timer or you want just one easter egg to spawn ?
Reply
#3

Quote:
Originally Posted by Golimad
Посмотреть сообщение
Код:
new Float:EasterEggs[][]= 
{ 
    {X, Y, Z}, 
    {X, Y, Z}, 
    {X, Y, Z}, 
    //etc.. 
}; 

stock SpawnEasterEgg()
{
new randomCoord, SelectCoord; 
}
Uh..whut ?

Sorry,but i don't get it ..

So,nothing that i did works ?Oh man..i'll test it,anyway,thank you for the reply.

I was planning to spawn all the easter eggs,i only got problems with the random func..
Reply
#4

I think what you are trying to do is spawn all of them but you don't want many easter eggs in one spot, is that right ?
Reply
#5

Quote:
Originally Posted by Golimad
Посмотреть сообщение
I think what you are trying to do is spawn all of them but you don't want many easter eggs in one spot, is that right ?
Exactly,I don't want that the Easter Eggs spawns at the same coord from others,for example.

One EE per coordinate.
Reply
#6

Код:
new Float:EasterEggs[][]= 
{ 
    {X, Y, Z}, 
    {X, Y, Z}, 
    {X, Y, Z}, 
    //etc.. 
}; 
stock SpawnEasterEgg() // Will spawn em all in one time. 
{
      for(new i=0;i<sizeof(EasterEggs);i++)
      {
             CreateEE(EasterEggs[i][0],EasterEggs[i][1],EasterEggs[i][2]);
      }
}
Is this what you are looking for or you want to spawn an easter egg, wait some minutes and then spawn the other one ?
Reply
#7

That sytem will not prevent them from spawn in the same coordinates,this is waht i'm trying to prevent.

I prefer that they all spawn togheter,but preventing repeated coordinates,surely that the code that i post didn't work properly ?
Reply
#8

The code I gave at last will spawn them all - same time - each one in its own coords - no repetition.
If you want them to spawn one by one during a timer with random function, use something this way :
new EasterEggs[][]=
{
{Float:X, Float:Y, Float:Z, bool:SpawnedAlready},
{Float:X, Float:Y, Float:Z, bool:SpawnedAlready},
{Float:X, Float:Y, Float:Z, bool:SpawnedAlready},
//etc..
};
run a random function, if the array has SpawnedAlready==false then spawn it in that position and set it true, if it has just do a while function until you find some array with false:SpawnedAlready, I hope you understood what I meant
Reply
#9

Very Nice!

But are you sure that it will work for every coordinate ?
It will not give a error "already defined" or work for all the coordinates instead of one ?
Reply
#10

Quote:
Originally Posted by Golimad
Посмотреть сообщение
run a random function, if the array has SpawnedAlready==false then spawn it in that position and set it true, if it has just do a while function until you find some array with false:SpawnedAlready, I hope you understood what I meant
Lets say there is only item left all other are set to true, if you are unlucky the random function will never find that number and hang your server

Quote:
Originally Posted by Detoria
Посмотреть сообщение
That sytem will not prevent them from spawn in the same coordinates,this is waht i'm trying to prevent.

I prefer that they all spawn togheter,but preventing repeated coordinates,surely that the code that i post didn't work properly ?
It will not even go past
pawn Код:
if(SelectCoord == sizeof(cCheck[i]))
because that isn't valid code

You want to randomize your easter eggs but how should they be randomized if all coordinates are used and all easter eggs look the same ?
If you only want to spawn x (x < max coords) without duplicates that should be no problem
Or do you want to spawn one easter egg without using the already spawn position till all positions are used

Just explain more precise what you want
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)