CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    1

    Random number generator

    Ok so im helping out my local gaming center by writing a program that allows me to input the number of teams that are participating in a tournament, then output what bracket line they will be on for the brackets tournament. I have everything working except the numbers repeat, I need a way to check and "re-roll" so the numbers never match. I know i can use a for loop but its been almost 4 years since ive written a program. I was also thinking if there is a way i can just fill the array and then shuffle it and print the numbers that way. Any help is appreciated.

    #include <iostream>
    #include <stdlib.h>
    #include <ctime>
    #include <time.h>
    #include <algorithm>
    using namespace std;
    int main(int argc, char *argv[])
    {
    int x;
    cout<<"how many teams are in the tournament?"<<endl;
    cin>>x;
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    int i;//spot
    int t;//team

    int spots[x];
    i=0;
    t=1;
    while(i<x)
    {

    spots[i]=rand()%(x-1+1)+1 ;
    cout<<"Team "<<t<<" is on bracket line "<<spots[i];
    i=i+1;
    t=t+1;
    cout<<endl;
    }



    system("PAUSE");
    return EXIT_SUCCESS;
    }




    Its a little sloppy but it works

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Random number generator

    random_shuffle would be your best bet.

    http://www.cplusplus.com/reference/a...andom_shuffle/

  3. #3
    Join Date
    Apr 2010
    Posts
    20

    Re: Random number generator

    How large is the range of numbers you are generating? If the range of numbers you are generating are small to medium, maybe a bit array as boolean flags denoting if a number has been generated already?

    I didn't compile this, I just typed it in.

    Code:
    // Our range of numbers is say 0 to and including 1023, so we have
    // 1024 possible numbers, divide by 8 and we need 128 bytes for
    // out bit array
    
    struct BitArray
    {
        inline BitArray()
        {
            for (unsigned long i = 0; i < 128; ++i)
            {
                 Flags[i] = 0;
            }
        }
        unsigned char Flags[128];
    
        inline bool IsBitSet(unsigned long dwValue)
        {
             if (dwValue < 1024)
             {
                 const unsigned long dwByteIndex = dwValue / 8; // dwValue >> 3;
                 const unsigned long dwBitIndex = dwValue % 8; // dwValue & 7;
    
                 const unsigned char ucBit = 1 << dwBitIndex;
    
                 return (Flags[dwByteIndex] & ucBit) != 0;
             }
             else
             {
                 // throw an appropriate exception here
             }
        }
    
        inline void SetBit(unsigned long dwValue,bool bValue)
        {
            if (dwValue < 1024)
            {
                 const unsigned long dwByteIndex = dwValue / 8;
                 const unsigned long dwBitIndex = dwValue % 8;
    
                 const unsigned char ucBit = 1 << dwBitIndex;
    
                 if (bValue == true)
                 {
                     Flags[dwByteIndex] |= ucBit;
                 }
                 else
                 {
                     Flags[dwByteIndex] &= ~ucBit;
                 }
            }
            else
            {
                 // throw an appropriate exception here
            }
        }
    };
    
    int main(int argc,char * argv[])
    {
        srand(::GetTickCount()); // Probably should use CRT function, but whatever...
    
        int Spots[1024];
        int nCount = 0;
    
        BitArray myFlags;
    
        while (nCount < 1024)
        {
            const int nTemp = rand() % 1024; // rand() & 1023; // NOTE: Because of power of 2 could use AND
            // check if the bit is set
            if (myFlags.IsBitSet(nTemp) == true)
            {
                 continue;
            }
            else
            {
                Spots[nCount] = nTemp;
               myFlags.SetBit(nTemp,true);
               ++nCount;
            }
        }
    
        // whatever you need from here
    
        return 0;
    }

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Random number generator

    ^If you're going to take that approach, a std::bitset or a std::vector<bool> (which is not a true vector, and should be replaced by boost:ynamic_bitset if you have access to Boost) is simpler than rolling a new class.

    I agree that std::random_shuffle will probably be good enough though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured