By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Templatized 'Random' member function.

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote...
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
Jul 22 '05 #2

P: n/a
"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/
Jul 22 '05 #3

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:5s8jd.304019$wV.177619@attbi_s54...
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.