help!!

This is a discussion on help!! within the C++ Programming forums, part of the General Programming Boards category; How can I get random numbers without repeated?? What I don't know is how can I check the repeated numbers. ...

  1. #1
    Unregistered
    Guest

    help!!

    How can I get random numbers without repeated?? What I don't know is how can I check the repeated numbers. Any examples of checking repeated numbers?? thx!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > How can I get random numbers without repeated?
    Store the ones you've generated in an array.
    When you generate a new random number, scan the array to see if you've seen it before
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    I did what u do... However, it doesn't work..... Can u help me to correct it?? What I wrote:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    inline void randomize(){
    	srand((unsigned)time(NULL));
    }
    
    int main(void){
    	
    	int num1,num2[5],j=0;
    
    	randomize();
    
    for (int i=0; i<5; ++i){
    
    
    		while(j < 5){
    		num1 = (rand() % (1000-100)) + 100;
    	
    			while((num1 == num2[0])||(num1 == num2[1])||(num1 == num2[2])||(num1 == num2[3])||(num1 == num2[4])||(num1 == num2[5])){
    				num1 = (rand() % (1000-100)) + 100;
    			}
    	
    		  cout << num1 <<" ";
    		  num2[j] = num1;
    		  j++;
    		}
    	cout <<endl;
    	
    	}
    	return 0;
    }
    Thanks!!

  4. #4
    Unregistered
    Guest

    Please!! Help!!

    I can't work it out!! What's wrong with my codes?? a help would be appreciated! Thanks!!

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    inline void randomize(){
    	srand((unsigned)time(NULL));
    }
    
    int main(void){
    	
    	int num1[9],num2 = 1;
    
    	randomize();
    	for(int i=0;i<10; i++){
    		num1[0] = (rand() % (1000-100)) + 100;
    		do{
    			for(int i=0;i<num2;i++){
    				if (num1[num2]==num1[i]) break;;
    				if (i==num2) num2++;
    				
    				
    			}
    		}while (num2<9);
    		
    	}
    	for(int j=0;j<9;j++){
    		cout << num1[j];
    	}
    	cout<<endl;
    	return 0;
    }

  5. #5
    Unregistered
    Guest
    The codes above are for random! I think the checking unrepeated numbers part is wrong!!

  6. #6
    Unregistered
    Guest
    I change again, but still can't work!! Please help me!! Thanks!!

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    inline void randomize(){
    	srand((unsigned)time(NULL));
    }
    int check(int num1,int i);
    
    int main(void){
    	
        
    	int num1 = 0;
    
    	randomize();
    
    	for (int i=0; i<5; ++i){
    		num1 = rand() % 7;
    		check(num1,i);
    		cout << num1 <<" ";
    	}
    	cout <<endl;
    	return 0;
    }
    int check(int num1,int i){
      int num2[5];
    	for (int j=0; j<i; j++){
    		while (num1 == num2[j]){
    		num1 = rand() % 7;
    		check(num1,i);
    		}
    		num2[j] = num1;
    		}
    	return (num1,i);
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX_UNIQUE 10
    
    // is the new random number in the 'num' previous old random numbers
    int seen_before ( int old_rands[], int num, int new_rand ) {
        int i, seen = 0;
        for ( i = 0 ; i < num && !seen ; i++ ) {
            seen = old_rands[i] == new_rand;
        }
        return seen;
    }
    
    int main ( ) {
        int seen[MAX_UNIQUE];
        int i, r;
        srand(time(NULL));
        for ( i = 0 ; i < MAX_UNIQUE ; i++ ) {
            do {
                r = rand() % 100;
            } while ( seen_before( seen, i, r ) );
            seen[i] = r;
            printf( "%d ", r );
        }
        return 0;
    }
    > inline void randomize(){
    Why?
    What is the point of inlining a one line function, which is only called once?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Unregistered
    Guest
    alternate version in pseudocode

    main()
    int num1;

    //array to store random numbers, initialized to -1
    int numbers[5] = {-1};
    int i;

    //to keep track of how many unique random numbers found
    int index = 0;

    //flag indicating random number is unique in array.
    char found;

    //seed random number generator using srand() or randomize
    srand()

    //want to find 5 unique rand numbers
    while index < 5

    //set flag each time through loop
    found = 'n';

    //generate random number
    num1 = rand % 100;

    //check this random number against numbers in array
    for(i; i < 5; i++)
    {

    //if ranom number already in array
    if numbers[i] == num1;

    //change flag
    found = 'y'
    }

    //if flag not changed then this random number is unique
    if found == 'n'
    {

    //so add it to the array at the current index
    number[index] = num1;

    //and increment the index for next number
    index++;
    }
    end while

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21