Like Tree1Likes
  • 1 Post By Subsonics

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

This is a discussion on To DOnt repeat the same numbers ... (ARRAYS) within the C Programming forums, part of the General Programming Boards category; hi my programme is this one Code: #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> int main (){ int N=100; ...

  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
    21,749
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.
    stahta01 likes this.

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, 05: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, 10:26 AM
  5. arrays and random numbers
    By bruceness in forum C Programming
    Replies: 3
    Last Post: 10-23-2002, 09:36 PM

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