473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Templatized 'Random' member function.

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
3 1936
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hall | last post by:
I have a problem with my design of a templatized class. I'm trying to figure out how to load and save the data inside it, but can't. My class looks like this ------------------------------------...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
13
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal...
1
by: StephQ | last post by:
I can't get this code working: class Scheme_euler { private: .... public: ....
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
2
by: mrstephengross | last post by:
Hi folks! I'm trying to create a function pointer to a templatized, static, overloaded member function. My class (Mgr) has two functions with the same name: "go". The first takes a T reference (T...
13
by: Bruza | last post by:
I need to implement a "random selection" algorithm which takes a list of as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of...
20
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.