Jump to content

Same results each time using srand() & rand()


Recommended Posts


I am getting the same sequence of random numbers every time I run my SystemC code ... there is too much code to post here, so I will attempt to describe what I am doing ...

I have written a DUT (SC_MODULE) with 9 input ports and I have written a Driver (SC_MODULE) to drive one of those ports, I have instantiated 9 Drivers and 1 DUT in sc_main ... the Driver uses rand() ...

Each Driver produces the exact same sequence of random numbers ...

I didn't some looking on the C++ forums and tried the following ...

1. Adding srand(time(NULL)) to the Driver ... but it won't compile

2. Adding srand(time(NULL)) to just sc_main ... compiled ok but made no difference

3. Adding srand(time(NULL)) to the constructor, I added it to the Driver SC_CTOR ... compiled ok but again made no difference

Those were the only possible solutions I found on the C++ forums ... I didn't see anything related on here ...

As always any and all help would be greatly appreciated :-)

Link to comment
Share on other sites

Hi @rayzekus,

Can you post details about your build environment? (e.g: Which OS and Which Compiler and also the configuration used to Create SystemC library)

As per the info available here.


Data races

The function accesses and modifies internal state objects, which may cause data races with concurrent calls to rand or srand.

Some libraries provide an alternative function that explicitly avoids this kind of data race: rand_r (non-portable).

C++ library implementations are allowed to guarantee no data races for calling this function.

It looks like the data race condition is getting triggered. I would not trust the libraries when utilizing any C based API's from C++.

I also tried your code locally on my system, and even I am getting the random number generated.

Also you are missing the following headers in your files:

// In File sc_main.cpp
// For srand declaration.
#include <cstdlib>
// For time()
#include <ctime>

// In file foo_drvr.h
// For rand() function.
#include <cstdilb>

I would suggest you look into another random number generator such as boost::random, or if you are on Linux System you can also look into /dev/random or /dev/urandom files which are used for these purposes.

Best Regards,

Ameya VIkram Singh

Link to comment
Share on other sites

Hi Ameya,

Thanks, your reply sounds like what is happening ... I am just a novice at this so here is what I know about my build environment ... I am running on a Window7 laptop, I compiled the SystemC library with Cygwin g++ (note: I am now compiling my code with g++ -std=c++11 not just g++) ... I write and compile my code in Eclipse(Neon) ...

Q: I didn't quite understand your statement ...

9 hours ago, AmeyaVS said:

I also tried your code locally on my system, and even I am getting the random number generated.

Are you saying you are getting random numbers generated with my code (unmodified)?


Link to comment
Share on other sites

Hello @rayzekus,

I tried your source code with slight modifications(no functional changes) on Linux and it is working as it is.

I think due to the nature of Cygwin Environment which creates a emulated POSIX layer is not fully compliant.

Even I have faced similar issue regarding random function but it was sometime back and I had then moved onto using other random number generator.

Just a quick question about the SystemC library configuration did you enable the "pthreads" option for compiling on Cygwin?

Best Regards,

Ameya Vikram Singh


Link to comment
Share on other sites

Hello @rayzekus,

Thank you for the update.

7 minutes ago, rayzekus said:

Do you know if GNU g++ would be better?

It's a personal preference in my judgement, but I work on most compilers(GCC, Clang, MSVC, etc.)

Regarding using G++ on Cygwin has it's own problems of performance hit due to emulating the Linux/POSIX environment.


Ameya Vikram Singh

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...