|
-
May 24th, 2010, 01:54 PM
#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
-
May 24th, 2010, 03:04 PM
#2
Re: Random number generator
-
May 25th, 2010, 01:17 PM
#3
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;
}
-
May 25th, 2010, 02:29 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|