473,671 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL function random_shuffle( ) possible bug?

Hi,
I'm encountering a very erratic behavior of STL function
random_shuffle( ) in my code. I have vectors s1 and s2 each having
elements {1, 2, 3, 4, 5, 6} which i want to randomly shuffle.

My problem is that random_shuffle( ) (with or without the third
parameter) replaces any element of either vector with 0. for example
after some random_shuffles , i get

Sequence S1: { 1 5 2 6 4 0 }, Sequence S2: { 1 3 5 4 2 6 }
OR
Sequence S1: { 3 2 5 6 4 0 }, Sequence S2: { 4 5 3 2 6 1 }

This is the code I wrote to do random_shuffle( )

void gen_new_sequenc e () {

MyRandom rd;
srand((unsigned )time( NULL));
random_shuffle( s1.begin(), s1.end( ), rd);

srand((unsigned )time( NULL ));
random_shuffle( s2.begin( ), s2.end( ), rd);
}

The function object MyRandom is defined as follows:

class MyRandom { //random number generating functor
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<dou ble>(rand()) /
static_cast<dou ble>(RAND_MAX);
return static_cast<ptr diff_t>(tmp * max);
}
};

One thing i noted is that if vectors s1 and s2 have a large size, the
problem doesn't occur. For smaller sized vectors, a 0 gets invariably
inserted somewhere.

Can anyone help?
Jul 19 '05 #1
1 4021
On 16 Nov 2003 13:12:57 -0800, ro***********@y ahoo.com (Ronak) wrote:
Hi,
I'm encountering a very erratic behavior of STL function
random_shuffle () in my code. I have vectors s1 and s2 each having
elements {1, 2, 3, 4, 5, 6} which i want to randomly shuffle.
random_shuffle is a simple algorithm. You should be able to tell by
inspection of the source code that it is correct - I severely doubt
such a simple algorithm would contain a bug.
My problem is that random_shuffle( ) (with or without the third
parameter) replaces any element of either vector with 0. for example
after some random_shuffles , i get

Sequence S1: { 1 5 2 6 4 0 }, Sequence S2: { 1 3 5 4 2 6 }
OR
Sequence S1: { 3 2 5 6 4 0 }, Sequence S2: { 4 5 3 2 6 1 }

This is the code I wrote to do random_shuffle( )

void gen_new_sequenc e () {

MyRandom rd;
srand((unsigned )time( NULL));
random_shuffle( s1.begin(), s1.end( ), rd);

srand((unsigned )time( NULL ));
You should only generally call srand once in your whole program (often
the first line of main).
random_shuffle( s2.begin( ), s2.end( ), rd);
}

The function object MyRandom is defined as follows:

class MyRandom { //random number generating functor
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<dou ble>(rand()) /
static_cast<do uble>(RAND_MAX) ;
The above line is wrong, since rand() == RAND_MAX is possible. It
should be:

tmp = rand() / (RAND_MAX + 1.0);

return static_cast<ptr diff_t>(tmp * max);
}
};

One thing i noted is that if vectors s1 and s2 have a large size, the
problem doesn't occur. For smaller sized vectors, a 0 gets invariably
inserted somewhere.

Can anyone help?


The random functor has to behave as specified - it must return a
number in the range [0, max). Yours was returning [0, max] (e.g.
inclusive at both ends). I suspect that this is the problem, but it
might not be.

Tom
Jul 19 '05 #2

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

Similar topics

9
4953
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
19
12108
by: Peter Ammon | last post by:
I'm calling random_shuffle without passing in a RandomNumberGenerator and getting the same shuffle every time I restart my program. Apparently I need to seed the internal random number generator. But how? I can't find any way to do that, and if there is none, then how is the internal random number generator useful? Thanks, -peter
1
3972
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
1
4718
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length (nrow x ncol) and every element of the vector contians its pixel value. vector float V ;
3
1787
by: Dave | last post by:
Hello all, When working with the STL, under what circumstances may I use a function object that modifies its internal state from one call to the next? I know this can be done with for_each<>(). I know it cannot be done if the function object happens to be a predicate. Is there a general rule that says when a function object can or cannot modify state?
7
3243
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
18
5919
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. More holidays can be added easily. Also, there is a boolean flag for including or excluding Saturdays or Sundays as part of the work week. The only thing I know of that is missing is that some holidays are observed on a Friday or a Monday if they...
1
2785
by: Generic Usenet Account | last post by:
I had a need to create my own RandomNumberGenerator class, for use with random_shuffle (I did not want the same sequence generated each time). I looked up an example provided by Nicolai M. Josuttis, in his fantastic book : The C++ Standard Library - A Tutorial and Reference (http://www.josuttis.com/libbook/). Here is what he had: class MyRandom { public: ptrdiff_t operator() (ptrdiff_t max) { double tmp;
8
1842
TimNick90
by: TimNick90 | last post by:
I am making a hangman program for school that uses a function for the player to input their guess and a seperate function to test whether or not their guessed letter is in the word. The program worked fine without the function to test the players guess but when i copied the code from the main program and turned it into the function i had to change most of the variables to global. All but one is working, my "soFar" string is supposed to change...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8597
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
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 we have to send another system
2
2048
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.