Thread: To DOnt repeat the same numbers ... (ARRAYS)

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    To DOnt repeat the same numbers ... (ARRAYS)

    hi

    my programme is this one
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    
    int main (){
             
             int N=100;
             int num[N];
             int i;
             int contnum=1;
        
             //inizio algortimo
        
             srand((unsigned)time(NULL));
             num[0]=1;
             num[99]=100;
             
        
        for(i=0;i<N;i++){
                            
             num[N]=rand()%(num[99]-num[0]+1)+num[0];
             printf("\n%d ---> %d",contnum,num[N]);
             contnum++;
             _sleep(100);
        }
             
        
             printf("\n\n\n");
             //system("pause");
             getch();
    }
    It Shows the Numbers extracted casually by PC . ( 1<=number<=100 ).

    but how can i dont make it repeat the already extracted number ???
    please help ... need it for tomorrow lesson

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are two general approaches:

    1. Save the numbers generated, e.g., into an array. When generating a new number, check if it has been generated. If so, re-generate, otherwise accept it.
    2. Populate an array with the numbers. Shuffle the array.

    #1 is usually better when there are only a few random numbers required compared to the range of possible random numbers. #2 is usually better when there are many random numbers required compared to the range of possible random numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    thanx ... but can u explain me with an example ... because im a starter .... and i am not even tht good with English ....

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    For method 2 there is an algorithm known as "Fisher-Yates shuffle", that also makes sure that the result is unbiased. I mentioned it the last time you asked, wikipedia has a clear explanation, which is also the first Google result.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generate non repeat numbers
    By abyss in forum C Programming
    Replies: 20
    Last Post: 10-27-2009, 09:51 AM
  2. sum of n numbers using arrays
    By IPCHU in forum C Programming
    Replies: 4
    Last Post: 12-07-2006, 06:05 AM
  3. detecting repeat numbers
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2003, 12:19 PM
  4. if you dont like linux threads, dont read...
    By ... in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-03-2003, 11:26 AM
  5. arrays and random numbers
    By bruceness in forum C Programming
    Replies: 3
    Last Post: 10-23-2002, 09:36 PM