Hi,
I want to display integers 0-99 in randomize order. I can get the list on integers, but can not do in randomize order, I used rand and srand, but it’s not giving me unique numbers. Can anyone help me? -thanks

Here is what I did so far…

//* This is to get the 0-99 integers….*//





#include <iostream>


using std::cout;
using std::endl;


#include <iomanip>



int main()
{
int n[ 99 ];


for ( int i = 0; i < 99; i++ )
n[ i ] = 0;


cout << "Integer" << endl;



for ( int j = 0; j < 99; j++ )
cout << j << endl;


return 0;


}


//* Randomizing numbers…*//


#include <stdio.h>
#include <stdlib.h>


int main()
{
int i;


for (i = 0; i < 10; i++) {
printf("%d\n",rand()%10);
}


return 0;

Edited 3 Years Ago by happygeek: fixed formatting

What about this one? It is a small modification of your second program. It displays 99 or 100 numbers but there is no guarantee that all the numbers from 0 to 99 will be output.

//* Randomizing numbers…*//

#include <stdio.h>
#include <stdlib.h>

int main()
{
    srand( time(0));
    int i;

    for (i = 0; i < 100; i++) 
    {
        printf("%d\n",rand()%100);
    }
    return 0;
}

thanks, WolfPack
.. but still it's not giving me unique numbers... some numbers are being displayed more than once...

Yes I know. So you have to store the numbers that have come up before and refrain from displaying them if they come up again. To do that

int Array [ 100 ] = { 0 };
int counter = 0;
int number;
while ( counter < 100 )
{
        number = rand()%100;
        if ( Array[ number ] == 0 )
        {
               printf ("%d\n", number);
               Array[ number ] = 1;
               counter++;
        }
}
This article has been dead for over six months. Start a new discussion instead.