Templatized 'Random' member function.

M

ma740988

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.
 
V

Victor Bazarov

ma740988 said:
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;
}

I would rather see

#include <cstdlib>
#include <cstddef> // for 'ptrdiff_t'

template<class T> struct random_helper { typedef T diff; };
template<class T> struct random_helper<T*> { typedef std::ptrdiff_t diff; };

template<typename T> T random(T b, T t)
{
typedef random_helper<T>::diff D;
return b + D(double(std::rand())/(RAND_MAX+1.0)*(t-b+1));
}

// which should allow me to pick a random ptr in a range of two ptrs

#include <iostream>
using namespace std;

int main()
{
char str[] = "abcdefghijklmnopqrstuvwxyz";

for (int i = 0; i < 50; ++i)
cout << *random(str, str+25);
cout << endl;

for (int i = 0; i < 50; ++i)
cout << random(1, 9);
cout << endl;
}
------------------------------------------------------------------------

As to coversions, I probably don't understand the issue.
}

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?

Clear nonsense. I just showed how it could be used with pointers.
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...?

I have no idea what that means.
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.

I am not smart enough either. Perhaps your colleagues could shed more
light on the "problem".

V
 
C

Cy Edmunds

ma740988 said:
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.
 
C

Cy Edmunds

Victor Bazarov said:
ma740988 said:
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;
}

I would rather see

#include <cstdlib>
#include <cstddef> // for 'ptrdiff_t'

template<class T> struct random_helper { typedef T diff; };
template<class T> struct random_helper<T*> { typedef std::ptrdiff_t
diff; };

template<typename T> T random(T b, T t)
{
typedef random_helper<T>::diff D;
return b + D(double(std::rand())/(RAND_MAX+1.0)*(t-b+1));
}

// which should allow me to pick a random ptr in a range of two ptrs

#include <iostream>
using namespace std;

int main()
{
char str[] = "abcdefghijklmnopqrstuvwxyz";

for (int i = 0; i < 50; ++i)
cout << *random(str, str+25);
cout << endl;

for (int i = 0; i < 50; ++i)
cout << random(1, 9);
cout << endl;
}

[snip]

Clever, but if random just returned an integer you could still write

cout << str[random(0,25)];

which if anything may look a little clearer. Basically, the concept of
selecting from a range of discrete choices and the type int seem like a good
natural match.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top