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...
Array = rand(50);
if ( er... sorry, I'm lost...
int randArray[5];
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;
}
}
}
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.
int j; int i;
for(i=0;i<5;i++)
{
randArray[i]=-1; // this is to prevent 0 from always being last
}
for(i=0;i<5;i++)
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;
}
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;
}
}
Users browsing this forum: No registered users and 1 guest