Hi,
I made this simple program. -
UINT Func(void * x)
-
{
-
cout << rand() << endl;
-
return 0;
-
}
-
-
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
-
{
-
int nRetCode = 0;
-
-
srand(time(0));
-
-
while(1)
-
{
-
Sleep(10);
-
AfxBeginThread(Func, NULL);
-
//Func(NULL);
-
}
-
-
return nRetCode;
-
}
-
-
Now, All that is printed on the console is a steady stream of 41s. I guess this is because the rand() function is somehow "reset" for different threads. But waht I want to get is actual random numbers for each thread. How can I achieve this? Thanks.
8 3766 Banfa 9,065
Expert Mod 8TB
Write you own thread re-entrant rand function.
You should be able to find a workable algorithm using Google or Wikipedia.
You can make the function thread re-entrant by using a CriticalSection.
I did modify it by using critical sections, but still the same problem. I don't feel like writing my own rand function. Isn't there anyway to get this done?
here's the modified version: -
CRITICAL_SECTION crtsec;
-
-
static int Rand()
-
{
-
return rand();
-
}
-
-
UINT Func(void * x)
-
{
-
EnterCriticalSection(&crtsec);
-
srand(timeGetTime());
-
cout << rand() << endl;
-
LeaveCriticalSection(&crtsec);
-
return 0;
-
}
-
-
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
-
{
-
InitializeCriticalSection(&crtsec);
-
int nRetCode = 0;
-
-
srand(time(0));
-
-
while(1)
-
{
-
Sleep(10);
-
AfxBeginThread(Func, NULL);
-
//Func(NULL);
-
}
-
-
DeleteCriticalSection(&crtsec);
-
return nRetCode;
-
}
-
Banfa 9,065
Expert Mod 8TB
I did modify it by using critical sections, but still the same problem. I don't feel like writing my own rand function. Isn't there anyway to get this done?
Not really unless you have a separate thread which serves random numbers to the other threads so that only 1 thread is calling rand. This would be more complecated than writing your own rand function.
The critical sections are not working in this instance because they have no effect on the code they contain all they do is prevent 2 threads running the same bit of code at the same time which would be required if that bit of code was using some static data to prevent data corruptions by multiple accesses to that data.
Honestly writing your own rand function just isn't that hard and I suspect if you check the references I gave you will find the code or pseudo code for one without having to do much writing at all.
So, I'll try my hand at a rand function of my own then. Thanks.
Why not take the random number from the last call and use it as an argument to your thread?
That way your thread could call srand() using the argument followed by
rand() to to get the next random number.
Banfa 9,065
Expert Mod 8TB
Why not take the random number from the last call and use it as an argument to your thread?
That way your thread could call srand() using the argument followed by
rand() to to get the next random number.
I don't think that would work in this case were a lot of threads are being started asynchonously.
EDIT: Actually I take this back it would with use of critical section.
I don't think that would work in this case were a lot of threads are being started asynchonously.
In that case you could use a server thread to generate the random numbers. This thread would just idle out there looking in a mailbox for a random number request. It could out the random number inthe mailbox. All of the other threads just use the mailbox in a critical section.
Banfa 9,065
Expert Mod 8TB
In that case you could use a server thread to generate the random numbers. This thread would just idle out there looking in a mailbox for a random number request. It could out the random number inthe mailbox. All of the other threads just use the mailbox in a critical section.
You could do this, in fact I said so. But I think it is easier to write your own PRNG and put it in a critical section.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tom McCallum |
last post by:
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's...
|
by: eyal.susser |
last post by:
I hear rand() is not thread safe. I was using it, foolish man that I
am. But what is meant exactly by unsafe? What can happen? Bizzare
results from rand()? Something worse?
Thanks,
Eyal.
|
by: Profetas |
last post by:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks
|
by: Jack |
last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that
the same value will not appear twice?
And is this a floating window?
For example, if RAND_MAX is 32767, and I make...
|
by: Roka |
last post by:
Hi all.
I'm reading a program which used the sentence below:
#define NUM_THREADS 10
... ...
int rand_num;
rand_num = 1+ (int) (9.0 * rand() / (RAND_MAX + 1.0));
sleep(rand_num);
... ......
|
by: Siam |
last post by:
Hi all,
I'm writing a shell language in c++ that supports the generation of
random numbers, and by its nature, each command must be executed in a
new thread. The call to the random function from...
|
by: ds |
last post by:
Hi all,
rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with...
|
by: Rafael Cunha de Almeida |
last post by:
Hi,
I've found several sites on google telling me that I shouldn't use
rand() % range+1
and I should, instead, use something like:
lowest+int(range*rand()/(RAND_MAX + 1.0))
|
by: Rich Fife |
last post by:
Quick rand() question:
I know you're not supposed to use "rand() % 1024" for instance,
because it focuses on the lower bits. However, it seems to me that
given that the argument is not a power...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |