Page 1 of 1

random numbers and not repeat??

PostPosted: Thu Jul 02, 2009 10:37 am
by futuro
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...

Re: random numbers and not repeat??

PostPosted: Thu Jul 02, 2009 11:25 am
by skydereign
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.

Re: random numbers and not repeat??

PostPosted: Thu Jul 02, 2009 1:37 pm
by futuro
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.

Re: random numbers and not repeat??

PostPosted: Sat Jul 04, 2009 2:18 pm
by futuro
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

Re: random numbers and not repeat??

PostPosted: Sat Jul 04, 2009 7:59 pm
by skydereign
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.

Re: random numbers and not repeat??

PostPosted: Sun Jul 05, 2009 8:30 am
by futuro
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

Re: random numbers and not repeat??

PostPosted: Mon Jul 06, 2009 5:27 pm
by futuro
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?

Re: random numbers and not repeat??

PostPosted: Wed Jul 15, 2009 10:46 am
by rosetaylor01
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

Re: random numbers and not repeat??

PostPosted: Wed Jul 15, 2009 12:01 pm
by skydereign
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;
  }
}