don't want to randomize two alike numbers

This is a discussion on don't want to randomize two alike numbers within the C++ Programming forums, part of the General Programming Boards category; I have an annoying problem. I want to randomize 7 different number between 1-35 (I know how to randomize). The ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    don't want to randomize two alike numbers

    I have an annoying problem.

    I want to randomize 7 different number between 1-35 (I know how to randomize).

    The problem is: How can I randomize so I won't have two numbers alike?

    I have tried vectors, lists static reference and everything. But I can't figure it out

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    OK, try using sets, keep inserting random numbers until set size is 7
    Code:
    #include <set>
    
    int main()
    
    set<int> random7;
    while (random7.size() < 7)
    {
         random7.insert( PutYourRandomNumberFunctionHere())
    }
    Last edited by Darryl; 05-24-2005 at 02:45 PM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Only issue with the set method is that you end up with the random values in sorted order which might be bad depending what you need to do with the numbers... but you could take care of that in other ways.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by hk_mp5kpdw
    Only issue with the set method is that you end up with the random values in sorted order which might be bad depending what you need to do with the numbers... but you could take care of that in other ways.
    Ahh yea I didn't think about the sorting...the best thing to do is use vector, but just do a search of the vector first before you insert the next random number.

    Code:
    #include <vector>
    #include <algorithm>
    
    int main()
    {
        vector<int> random7;
        while(random7.size() < 7)
        {
            int RandomNumber = SomeRandomNumberFunction();
            if (find(random7.begin(), random7.end(), RandomNumber)==random7.end())
            {
                 random7.push_back(RandomNumber);
             }
         }
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a fun way:
    Code:
    int chosen[ FOO ] = {0}
    int x;
    
    for ( int y = 0; y < BAR; )
    {
        x = rand() % FOO;
    
        if( !chosen[ x ] )
        {
            chosen[ x ] = 1;
            y++;
        }
    }
    The index of the non-zero array elements is the chosen value. Add one to it if you don't like "0 through FOO -1".

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

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    2
    Thank you guys. I love you all!!!

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    I...I....i............love you too

    Now that we are done with that, if you want different sets of random numbers each time, remember to set the seed using the time function in the #include <ctime.h>

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by JoshR
    I...I....i............love you too

    Now that we are done with that, if you want different sets of random numbers each time, remember to set the seed using the time function in the #include <ctime.h>

    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by quzah
    Here's a fun way:
    Code:
    int chosen[ FOO ] = {0}
    int x;
    
    for ( int y = 0; y < BAR; )
    {
        x = rand() % FOO;
    
        if( !chosen[ x ] )
        {
            chosen[ x ] = 1;
            y++;
        }
    }
    The index of the non-zero array elements is the chosen value. Add one to it if you don't like "0 through FOO -1".

    Quzah.
    or we can use the possibility of vector<bool> being optimized:
    Code:
    std::vector<bool> chosen(FOO, false);
    int x;
    for ( int y = 0; y < BAR; )
    {
        x = rand() % FOO;
    
        if( !chosen[ x ] )
        {
            chosen[ x ] = 1;
            y++;
        }
    }
    Weee less memory

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 07:28 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 05:03 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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