C
Chris Dutrow
David White said:Chris Dutrow said:This still doesn't solve the problem though, heres why:
// get card
int card = deck[icard];
This assumes a data structure has been given that holds the set from which
the subset is derived from.
It doesn't assume that anything's been given. It's just an algorithm for
generating a unique subset. You could have a class called RandomSubset that
implements the algorithm I suggested, e.g.,
==============================>
Yeah, you're right, it doesn't assume that.
What I meant though is that the function uses a data structure that holds
the complete set of intergers.
Which is fine for this input range of
1 to 100
But very bad for an input range of
1 to 10000000000
So this implementation is not very versitile because it uses memory that an
optimal algorithm would not need to use.
class RandomIntegerSelector
{
std::vector<int> m_allValues;
==================>
See, this is what I have a problem with.
int m_nrValuesRemaining;
public:
// constructor fills m_allValues with the full set of values
==========================>
And this. What if a subset of length 10 is needed from an integer range of
0 to 10000000000000?
Then this implementation is wildly inappropriate.
RandomIntegerSelector(int begin, int end);
int NextRandomInteger();
};
The NextRandomInteger() member does the same as my CardDeck:ealCard()
member.
Your function then uses an object of this class to generate the subset:
std::vector<int> RandomSubsetOfIntegers( int beginRange, int endRange, int
subsetSize )
{
IntegerRandomSubset integerSelector(beginRange, endRange);
std::vector<int> subset;
int nrRemaining = subsetSize;
while(nrRemaining-- > 0)
subset.push_back(integerSelector.NextRandomInteger());
return subset;
}
DW
FYI,
what I'm doing here is designing an Artificial Neural Network. A Neuron in
the first layer links to a random number of Neurons in the second layer
between 1 and SecondLayer.size(). So I need a subset of integers that
correspond to the index's of the Neurons in the second layer to link to.
This Neural network could potentially be very large depending on the input
values so I don't want to create a large array of integers each time I need
to decided what each Neuron in Layer1 connects to.
-Chris