Thread: generate non repeat numbers

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    generate non repeat numbers

    hi, i need generate 5 diferent nubmers from 1 to 10, without repeating.

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define N 5
    void main()
    {
     int i,j,index;
     int mas[N];
     int random_integer;
    
    srand ( time(NULL) );
    for(index=0; index<5; index++){
    random_integer = (rand()%10)+1;
    
    mas[i]=random_integer;
      j=0;
      for ( i = 0; i< 5; i++ ) {
        if ( mas[j] == random_integer );
          break;
      }
    
      if ( j == i )
    		mas[i] = random_integer;
    		printf("%5d", mas[i]);
    }
         }

    i can`t understand how i should compare array elements, so in the output wouldnt be two indetical elements. Thank you

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Actually, I'd start with simply loading a 10 element array up with the numbers 1 through 10, and then just shuffling them, and picking off the first five.

    For 10 numbers, just brute-force generating randoms and testing for uniqueness is probably fine, but if you ever tried the same thing with a larger working set, you could be in a world of processor bound hurt.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    thanks , but i just want to understand and fix my error with my generator

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    i modified your code little bit this is also the one way which i thought

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX_NUMBER 10
    
    void main() {
      int index                    = 0;
      int number_table[MAX_NUMBER] = {0};
      int random_integer           = 0;
      int number_count             = 0;
    
      srand ( time(NULL) );
    
      while (number_count < 5) {
        random_integer = (rand() % 10) + 1;
        if (number_table[random_integer - 1] == 0) {
          number_table[random_integer - 1] = 1;
          ++number_count;
        }
      }
    
      index = 0;
      for (; index < MAX_NUMBER; ++index)
        if (number_table[index] == 1)
          printf("%d  ", index + 1);
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Rocky, that code will NOT generate a randomized 5 number selection. It will always be an 'In Order' selection, which removes quite a bit of the randomness.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Did u acually looked at the code there or you are just doing posing to increment the post count

    printing of random numbers will be in sequence and the genration will be in random order if you want that

    if you want printing also you can put the printf in while loop


    like the code below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX_NUMBER 10
    
    void main() {
      int index                    = 0;
      int number_table[MAX_NUMBER] = {0};
      int random_integer           = 0;
      int number_count             = 0;
    
      srand ( time(NULL) );
    
      while (number_count < 5) {
        random_integer = (rand() % 10) + 1;
        if (number_table[random_integer - 1] == 0) {
          number_table[random_integer - 1] = 1;
          ++number_count;
          printf("%d  ", random_integer);
        }
      }
    #if 0
      index = 0;
      for (; index < MAX_NUMBER; ++index)
        if (number_table[index] == 1)
          printf("%d  ", index + 1);
    #endif
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    thanks Rocky, your code works great

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Heheh, but to "generate 5 diferent nubmers from 1 to 10, without repeating", one just needs to generate 5 different numbers from 1 to 5, without repeating, e.g., 1, 2, 3, 4 and 5. Yay!

    Anyway, the aim is probably to randomly pick, without replacement, five of the integers in the range [1, 10]. My question is: does order matter?
    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

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    no, the order doesnt matter. But like I said, rocky wrote the code, which works excellent, an now i can move forward with my main task. anyway, thanks for trying help

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by abyss View Post
    no, the order doesnt matter. But like I said, rocky wrote the code, which works excellent
    Yes he has a very bad habit of doing everyone's homework for them.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by abyss View Post
    no, the order doesnt matter. But like I said, rocky wrote the code, which works excellent, an now i can move forward with my main task. anyway, thanks for trying help
    The naieve duplicate rejection method Rocky has used will work great for picking 5 numbers out of 10, but it will work awfully for picking 999 numbers out of 1000 as it'll spend too much time looking for unused slots when most of them are taken. In contrast, generating a random shuffle of n numbers and taking the first k numbers will work perfectly in that scenario, taking an amount of time that is always proportional to n.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    The naieve duplicate rejection method Rocky has used will work great for picking 5 numbers out of 10, but it will work awfully for picking 999 numbers out of 1000 as it'll spend too much time looking for unused slots when most of them are taken.
    If you want to pick 999 numbers out of 1000, it might make more sense to pick 1 to ignore then take the rest.

    Quote Originally Posted by iMalc
    In contrast, generating a random shuffle of n numbers and taking the first k numbers will work perfectly in that scenario, taking an amount of time that is always proportional to n.
    I think you only need to use a random shuffle that stops after k numbers are in their final position instead of shuffling the whole array.
    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

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    If you want to pick 999 numbers out of 1000, it might make more sense to pick 1 to ignore then take the rest.

    I think you only need to use a random shuffle that stops after k numbers are in their final position instead of shuffling the whole array.
    I hadn't read the part about the order not mattering. I was thinking that the numbers were in random order, hence picking one to leave out wouldn't get you very far. But yes assuming combinations that would make a world of sense!

    Upon implementing the shuffle method I would actually have performed only 1 swap for each number picked, but I was not going to complicate matters by delving into that level of detail.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    your code is also correct

    your actual code is also correct use mas[index]=random_integer instead of mas[i]=random_integer because you are using an uninitialized value of i .

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    thanks for all your advices . But i have one small problem. How to get all 5 values, after while? I can gen only last, maybe someone can help? for example, after while i have, if(random_integer=1) printf("done"), if (random_integer=2) printf("done2"), etc. If, generators, generates 1;2;3;4;5, i can use only 5, and i don`t know how to get all 5 values after while

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to generate random numbers the same everytime
    By lehe in forum C++ Programming
    Replies: 11
    Last Post: 04-09-2009, 06:00 PM
  2. Template metaprogramming, whats the point?
    By Cogman in forum C++ Programming
    Replies: 26
    Last Post: 02-01-2009, 11:47 PM
  3. My code wont generate randon numbers
    By n00b_101 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 05:39 AM
  4. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM
  5. Replies: 10
    Last Post: 11-23-2001, 10:01 AM