"ma740988" <ma******@pegasus.cc.ucf.edu> wrote in message
news:a5*************************@posting.google.co m...
Consider the template member function that generates a random number
within a range.
#include <cstdlib>
#include <ctime>
#include <iostream>
template<typename T>
T Random(T bottom, T top)
{
return T( (double(rand()) * (top - bottom + 1)) /
(double(RAND_MAX) + 1) ) + bottom;
}
From what I understand the code was found here, nonetheless it's been
the subject of discussion between two team members (call them A and B)
for two reasons:
1.
Team member 'A' claims that a random number generator in most of (if
not say 'all of') the cases returns integer or double values only,
(sometimes with chars but rarely) hence using template here is not
necessary?
2.
He also claims that his judgement might be wrong might be wrong by
proof of generating random objects of unknown types but casting (----)
to double then casting again to T would appear meaningless in the
'sense' of templatized parameters...?
Personally (Team member C :)) I dont see any issues with the code but
I'm not smart enough on random numbers to answer his claims.
Thanks for input.
It *is* kind of a silly template function. I would much rather see you
hardcode an int return and argument type than the use of std::rand(). 31
bits is enough for any application I ever had and there are plenty of good
portable 31 bit generators available. std::rand() is implementation
dependent and can provide as few as 15 bits.
Follow my sig and look up uvs for a different template approach. Also look
at
www.boost.org for their latest random number generators.
--
Cy
http://home.rochester.rr.com/cyhome/