Jump to content
rayzekus

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

Recommended Posts

Hi,

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 :-)

Share this post


Link to post
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.

Quote

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

Share this post


Link to post
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)?

Thx

Share this post


Link to post
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

 

Share this post


Link to post
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.

Regards,

Ameya Vikram Singh

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×