C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
need help in creating unique 100 nums with rand()%100 Posted by Vashudev on 2 Mar 2006 at 5:43 PM
Hi All!
I need to create and store a unique set of 100 integers from 0 to 99. I can't use srand, so my way of approach has been to:
--generate a number, compare it against all the numbers generated before, if any of those is euqal to the new number then generate the new number again and comparare..below is the what i have been trying to do...please help...thanks in advance
vashudev

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

int main(){
      
    int i = 0, j, iarray[100], randNum, counter = 0;
    
    randNum = rand()%100;
    iarray[i] = randNum;
    
    for( i = 1; i < 100; i++ ){
             
      randNum = rand()%100;
            
      for( j = 0; j < i; j++ ){
                 
         while( randNum == iarray[j] )
         
            randNum = rand()%100;
         
      }//end inner for
     
     iarray[i] = randNum;
     
    }//end outer for

for( i = 0; i < 100; i++ ){
  printf( "%-5d", iarray[i] );
  if( ++counter%10 == 0 )
    printf( "\n\n" );
}
  
system( "PAUSE" );
return 0;

}



Report
Re: need help in creating unique 100 nums with rand()%100 Posted by stober on 2 Mar 2006 at 6:42 PM
: Hi All!
: I need to create and store a unique set of 100 integers from 0 to 99.

just create a loop and store the loop counter in an array. There is no need for rand(), in fact it will only make thig worse.
int array[100];
for(int i = 0; i < 100; i++)
   array[i] = i;



Report
Re: need help in creating unique 100 nums with rand()%100 Posted by Vashudev on 3 Mar 2006 at 7:33 PM
Hi Stober!
Thanks for the idea but that doesn't solve my problem. I need to use the rand() function because I need random numbers at those array location, I know that at array[5] i have an integer 6, that's not random number. If you could look into my new code below and tell me what am i missing to make it work that would be great, please suggest...
thanks a lot
vashudev

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

int main(){
      
    srand( time ( NULL ) );
    int i = 0, j, k, iarray[10], randNum, counter = 0, numChecks = 0;
 
    iarray[i] = rand()%10;
    for( i = 1; i < 10; i++ ){
      
      randNum = rand()%10; 
      iarray[i] = randNum;
      printf("The number created each time for the for loop ");
      printf( "%-5d\n", iarray[i] );
      
          
            j = 0;

              while( j<i ){
                
                
                if( iarray[i] == iarray[j] )
                 while( randNum == iarray[j] ){
                    
                    randNum = rand()%10;
                    numChecks++;
                
                  }//end inner while
                
                j++;
                iarray[i] = randNum;//()%10;
                
               }// outer while
     
    }//end for


for( k = 0; k < 10; k++ ){

  printf( "%-5d", iarray[k] );

  if( ++counter%10 == 0 )

    printf( "\n\n" );
}

printf("The total number of checks performed is: " );
printf( "%-5d", numChecks );  
printf( "\n" );

system( "PAUSE" );
return 0;

}


: : Hi All!
: : I need to create and store a unique set of 100 integers from 0 to 99.
:
: just create a loop and store the loop counter in an array. There is no need for rand(), in fact it will only make thig worse.
:
: int array[100];
: for(int i = 0; i < 100; i++)
:    array[i] = i;
: 

:
:
:

Report
Re: need help in creating unique 100 nums with rand()%100 Posted by stober on 5 Mar 2006 at 9:15 AM
The solution is very simple -- generate a random number, search the array for it. If the number is already in the array then go back and generate another number. If however the number is not in the array then put it in there.
int main()
{
	int array[100] = {0};
	int LastUsed = 0;
	srand((unsigned int)time(0));
	int n = 0;
	int j = 0;
	for(int i = 0; i < 100; i++)
	{
		do {
			n = rand() % 100;
			for(j= 0; j < LastUsed; j++)
			{
				if(array[j] == n)
					break;
			}
		} while( j < LastUsed);
		array[LastUsed++] = n;

	}
	// display the array
	for(int i = 0; i < 100; i++)
	{
		cout << array[i] << " ";
		if( i > 0 && (i % 10) == 0)
			cout << endl;
	}
	cout << endl;
	cin.ignore();
	return 0;
}


Report
Re: need help in creating unique 100 nums with rand()%100 Posted by bilderbikkel on 3 Mar 2006 at 3:41 AM
The trick is to create the array like Stober said, then randomly shuffle the values:

#include <algorithm>

std::random_shuffle(&array[0],&array[100]);


See ya,
bilderbikkel

Report
Re: need help in creating unique 100 nums with rand()%100 Posted by Chainsaw666 on 4 Mar 2006 at 8:24 PM
Well, despite recieving two responses, I think maybe you'd understand better if it was like this.
int loop,number,check;
char array[100];
loop=number=check=0;
while(loop<100)
{
 number=rand()%100;
 while(check<100)
 {
  if(number==array[check])
  {
   if(number<100)
   { 
    number++;
   }
   else
   {
    number=0;
   }
   check=0;
  }
  else
  {
   check++;
  }
 }
 check=0;
 array[loop]=number;
 loop++;
}

That seems right to me, it's not tested, and I'm too lazy to read over it again, but I think that that is what you wanted, albiet fat and probably redundant, but like I said, I'm not gonna worry too much about that.



 

Recent Jobs