random numbers and not repeat??

Game Editor comments and discussion.

random numbers and not repeat??

Postby futuro » Thu Jul 02, 2009 10:37 am

well, I not know how to use one array, I think tath not dificult... I'm training in this

I need a few numbers (random) but not repeat any numbers, it's posible??

Pleasa help me...


Code: Select all
Array = rand(50);
if ( er... sorry, I'm lost...
alguien que hable mi idioma ÑñÑñÑñ
User avatar
futuro
 
Posts: 37
Joined: Mon Apr 13, 2009 1:22 pm
Score: 0 Give a positive score

Re: random numbers and not repeat??

Postby skydereign » Thu Jul 02, 2009 11:25 am

Don't know what you want, this is what I think you are asking; How do you set x values in an array to all random number, but no repeats. Here is one way, not sure it is the best, but it was the only thing to come to mind. Now that I think about it, not sure you should use it. It will work, but I am tired, so I'll get back to you with a better method, unless you don't care.

First is declaring your array, for this x will be 5.
Code: Select all
int randArray[5];


Next will be the setting.
Code: Select all
int i;
int j;
for(i=0;i<5;i++)
{
  randArray[i]=-1;   // this is to prevent 0 from always being last
}

for(i=0;i<5;i++)
{
 repeat:
 randArray=rand(5);
  for(j=0;j<5;j++)
  {
    if(randArray[i]==randArray[j] && i!=j)
    {
      goto repeat;
    }
  }
}


Not very straightforward... I could be wrong in what you want altogether, as you seem to be using Array as a single variable.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: random numbers and not repeat??

Postby futuro » Thu Jul 02, 2009 1:37 pm

skydereign wrote:Don't know what you want, this is what I think you are asking; How do you set x values in an array to all random number, but no repeats. Here is one way, not sure it is the best, but it was the only thing to come to mind. Now that I think about it, not sure you should use it. It will work, but I am tired, so I'll get back to you with a better method, unless you don't care.

First is declaring your array, for this x will be 5.
Code: Select all
int randArray[5];


Next will be the setting.
Code: Select all
int i;
int j;
for(i=0;i<5;i++)
{
  randArray[i]=-1;   // this is to prevent 0 from always being last
}

for(i=0;i<5;i++)
{
 repeat:
 randArray=rand(5);
  for(j=0;j<5;j++)
  {
    if(randArray[i]==randArray[j] && i!=j)
    {
      goto repeat;
    }
  }
}


Not very straightforward... I could be wrong in what you want altogether, as you seem to be using Array as a single variable.



ok, I work with this, after tell you...

That I need: five rand numbers not repeats.
alguien que hable mi idioma ÑñÑñÑñ
User avatar
futuro
 
Posts: 37
Joined: Mon Apr 13, 2009 1:22 pm
Score: 0 Give a positive score

Re: random numbers and not repeat??

Postby futuro » Sat Jul 04, 2009 2:18 pm

skydereign, not is that I want,

Code: Select all
   
    int j; int i; 

no problem to declare one or two variables

Code: Select all
for(i=0;i<5;i++)
    {
      randArray[i]=-1;   // this is to prevent 0 from always being last

array not is a random, array is five
Code: Select all
    }

    for(i=0;i<5;i++)

this is a ''for next''?, OK
alguien que hable mi idioma ÑñÑñÑñ
User avatar
futuro
 
Posts: 37
Joined: Mon Apr 13, 2009 1:22 pm
Score: 0 Give a positive score

Re: random numbers and not repeat??

Postby skydereign » Sat Jul 04, 2009 7:59 pm

That is not the random. The for loop increments, i=0, i<5, i++. So it is going through the array[5]. The random is done for 0 then 1 then 2. The first loop is there to prevent the last number from always being zero. rand picks from 0-4, if rand(5). It checks if all others are not the same as the rand(5), in this case 0, but if they are all zero, then the last will be. Sorry that did not make much sense.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: random numbers and not repeat??

Postby futuro » Sun Jul 05, 2009 8:30 am

don't worry, this I want:

press 'x' button, and show me one number (rand50), when push 'x' again a new number (rand50) is not repeat with the first.

if I need one array, when I push 'x' button the array contains five numbers not repeat and random.

Don't worry about your scored... :D
alguien que hable mi idioma ÑñÑñÑñ
User avatar
futuro
 
Posts: 37
Joined: Mon Apr 13, 2009 1:22 pm
Score: 0 Give a positive score

Re: random numbers and not repeat??

Postby futuro » Mon Jul 06, 2009 5:27 pm

Ok, I think that I got it...

Code: Select all
    int Alea; // Para un numero aleatorio
    int Ce; // Para el for next

    contaor = 0;
    linea30:
    Alea = rand(50);
 
    for (Ce = 0; Ce <5; Ce++)
    {
        if (Elarray[Ce] == Alea) goto linea30;
    }
    Elarray[contaor] = Alea;
    contaor = contaor + 1;
 
    if (contaor <5)
    {
        goto linea30;
    }
 
 


anybody that try if repeat any number?
Attachments
5randomnumbers.zip
windos executable
[Intro] to rand numbers
(820.69 KiB) Downloaded 139 times
alguien que hable mi idioma ÑñÑñÑñ
User avatar
futuro
 
Posts: 37
Joined: Mon Apr 13, 2009 1:22 pm
Score: 0 Give a positive score

Re: random numbers and not repeat??

Postby rosetaylor01 » Wed Jul 15, 2009 10:46 am

Is there a simple way to do this?

I want to pick 5 numbers between 1 and 35 randomly. But I want to make sure there is no chance for the same number showing up.

Correct 4,10,15,24,29

Incorrect 4,4,10,11,11
rosetaylor01
 
Posts: 3
Joined: Wed Jul 15, 2009 10:39 am
Score: 0 Give a positive score

Re: random numbers and not repeat??

Postby skydereign » Wed Jul 15, 2009 12:01 pm

It depends on what you consider easy, and how you are using the numbers. You could do this with simple code, or you can do it so it works simply with more complexity. This method is easier to implement. Just put this code into Global Code, then run the randNoRepeat function. The numbers are stored in the array random.
Code: Select all
int random[5];


int
randomGen(int steps)
{
  int i;
  int TRUE = 0;
  int RAND;
  do
  {
    RAND = rand(35);
    for(i=0;i<steps;i++)
    {
      if(RAND!=random[i])
      {
        TRUE=1;
      }
      else
      {
        TRUE=0;
      }
    }
    if(steps==0)
    {
      TRUE=1;
    }
  }
  while(TRUE==0);
 
  return(RAND);
}
 

void
randNoRepeat(void)
{
  int RAND;
  int i;
  for(i=0;i<5;i++)
  {
    RAND = randomGen(i);
    random[i]= RAND;
  }
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest