How to Generate Random Numbers but..

Well I want to generate random numbers. The problem is, I don't want them to repeat, and I want to get numbers with in range of 1-100.
for example
//

int RandomShapes()
{
int RandomShape;
int RandomShapeChoice;
RandomShape = rand() % 3 + 2;
RandomShapeChoice = RandomShape;

return 0;
}

//
this generates numbers 1-5, but if it generates random number 5, I don't want ever again to get number 5.
Is it even possible to do something like that.

I want this program to random up 100 times in range of 1-100, with out getting same number again.

thank you for your time :D
Last edited on
I was thinking to do this
//
int RandomShapes()
{
int RandomShape;
int RandomShapeChoice;
RandomShape = rand() % 3 + 2;

if (RandomShapeChoice == RandomShape)
{
int RandomShapes();
return 0;
}

RandomShapeChoice = RandomShape;

return 0;
}
//
but all this do is doesn't let you repeat the same number in row, but it will repeat if i get different number next, and same number as before.
Make int RandomShape into an array and test against it every time a new shape is made. This is an overly inefficent way of doing it but it's simple enough to play around with.
Fill an array with the integers 1 through 100.
Use a random number between 0 and 99 to index a number out of that array. (Now it has 99 elements)
Then another random number between 0 and 98 to index out the next number. (98 elements)
Loop until the array is empty...
Last edited on
Fill an array with the numbers 1-100.
Use std::random_shuffle() to randomize the order of the array.
Read the first N elements of the array (N being the number of random numbers you want).

Much less code this way.
Hi, I'm from Brazil and in 2006 i build this code:

(After creating a vector 'bufferr' with random int values...)

int cont_repetido = -1; i = 0; j = 0;

while (cont_repetido != 0)
{
cont_repetido = 0;
for (i = 0; i < tamanho; i++)
{
for (j = 0; j < tamanho; j++)
{
if (i != j)
{
if (bufferr[1][i] == bufferr[1][j])
{
cont_repetido = cont_repetido + 1;
bufferr[1][i] = 1 + rand() % maxrand;
}

}/*cout <<endl<<endl<<"Ok "<<cont_repetido<<endl; getch(); //Teste de erro*/
}
}
}

It's very useful for small values of maxrand (5 seconds for maxrand = 1000, 15 seconds for maxrand = 2000, and i wait more than 2 minutes for maxrand = 10000 and... nothing)
Topic archived. No new replies allowed.