I somewhat understand the concept of the parentheses overload when you need to use an object as a function. The question that I have is why does the max variable in the listed code below always input 2.
/*
Purpose: This is the header file of randomInteger.h
The class creates a random integer
Name: Saith
Date: 3/5/11
Edited: 5/28/11
*/
#ifndef RANDOMINTEGER_H
#define RANDOMINTEGER_H
#include<iostream>
using namespace std;
class randomInteger{
public:
unsigned int operator() (unsigned int);
unsigned time_seed();
};
#endif // RANDOMINTEGER_H
/*
Purpose: This is the implementation file, randomInteger.cpp
Name: Saith
Date: 3/5/11
Edited: 5/28/11
*/
#include<iostream>
#include<time.h>
#include"randominteger.h"
using namespace std;
unsigned int randomInteger::operator () (unsigned int max){
// QUESTION: Why is Max always 2? Or at least the times when I run/debug it.
unsigned int r;
do{
srand(time_seed());
r = rand();
}while ( max < r); // repeat while random number is greater than the maximum number.
return r;
}
unsigned randomInteger::time_seed() {
time_t now = time ( 0 );
unsigned char *p = (unsigned char *)&now;
unsigned seed = 0;
size_t i;
for ( i = 0; i < sizeof now; i++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[i];
return seed;
}
It is used in the context as such:
void Deck::shuffle(){
random_shuffle(deck, deck + topCard, randomizer);
}
Shuffle runs fine if I change the max value in the overload definition to 60, so it would read
while ( 60 < r); // repeat while random number is greater than the maximum number.
I've tried:
random_shuffle(deck, deck + topCard, randomizer(topCard));
but that's a very improper way to using overloaded parentheses. Is there any way I can change this default through the shuffle function, or any other way? Also, topCard is local to the class Deck, deck is a dynamic array to the class Deck as well.
Note: Thanks Narue for a better understanding of rand(). Grabbed it from her site, Eternally Confused.