473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tr1, random numbers

WP
Hello, I'm having some problems with the TR1 random number generators. I
need a function that takes a min and max value and generates a random
number within that range (inclusive). Here's my attempt:

int
random_number(int min, int max)
{
std::tr1::uniform_int<int>
distributor(min, max);
std::tr1::linear_congruential<unsigned long, 16807, 0, 2147483647>
generator;

generator.seed((unsigned long)time(NULL));

int n = distributor(generator);

cout << "Generated " << n << endl;

return n;
}

This function is called 50 times when the program is run with 3
different ranges and the problem is that it always returns the same
number for the same range. I am calling seed() on the generator so it's
not that. What am I doing wrong? I guess many of you won't be able to
run this code if you haven't got a very modern c++ compiler with tr1
implementation. This is my first time using tr1.

- WP
Jul 6 '08 #1
3 1997
WP wrote:
I am calling seed() on the generator so it's not that.
Incorrect. That's *exactly* the problem. You must *not* reseed the RNG
each time you want to get a value from it.

A RNG cannot be simply instantiated, a random value popped and then
destroyed. They don't work that way. You have to keep an instance
somewhere else, seed it only once, and then get values from it without
reseeding it.
Jul 6 '08 #2
On 2008-07-06 10:32:27 -0400, WP <in*****@invalid.invalidsaid:
Hello, I'm having some problems with the TR1 random number generators.
I need a function that takes a min and max value and generates a random
number within that range (inclusive). Here's my attempt:

int
random_number(int min, int max)
{
std::tr1::uniform_int<intdistributor(min, max);
std::tr1::linear_congruential<unsigned long, 16807, 0, 2147483647>
generator;

generator.seed((unsigned long)time(NULL));

int n = distributor(generator);

cout << "Generated " << n << endl;

return n;
}

This function is called 50 times when the program is run with 3
different ranges and the problem is that it always returns the same
number for the same range. I am calling seed() on the generator so it's
not that. What am I doing wrong? I guess many of you won't be able to
run this code if you haven't got a very modern c++ compiler with tr1
implementation. This is my first time using tr1.
Create the generator and the distribution object once, and call them
each time you need a number.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 6 '08 #3
WP
Juha Nieminen wrote:
WP wrote:
>I am calling seed() on the generator so it's not that.

Incorrect. That's *exactly* the problem. You must *not* reseed the RNG
each time you want to get a value from it.

A RNG cannot be simply instantiated, a random value popped and then
destroyed. They don't work that way. You have to keep an instance
somewhere else, seed it only once, and then get values from it without
reseeding it.
Yes, thanks alot for the quick reply!
Jul 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
11929
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
3
7351
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
21
2990
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
5
2385
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
104
5034
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
5195
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
21
13484
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
2767
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
11725
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
7173
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
7125
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,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.