Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2001-05-08
05:16 • Closed ticket [416643ffff]: rand() has identical results in threads. plus 7 ... artifact: 3b833c440c user: msofer
2001-05-05
05:31 • Ticket [416643ffff]: 1 change artifact: 4e561a73ae user: msofer
2001-04-30
01:35 • Ticket [416643ffff]: 6 changes artifact: 4cd3dec497 user: dgp
2001-04-26
08:01 • Ticket [416643ffff]: 5 changes artifact: d67b8d3180 user: davygrvy
2001-04-21
05:36 • Ticket [416643ffff]: 4 changes artifact: 176dc35986 user: davygrvy
2001-04-20
10:20 • Ticket [416643ffff]: 4 changes artifact: d990568eb2 user: davygrvy
10:20 • Add attachment rand_patch.txt to ticket [416643ffff] artifact: 3e908255a3 user: davygrvy
08:49 • Ticket [416643ffff] rand() has identical results in threads. status still Op... artifact: 28d86f9bf3 user: davygrvy
2001-04-18
06:15 • Ticket [416643ffff]: 4 changes artifact: 2957c71607 user: davygrvy
05:13 • Ticket [416643ffff]: 4 changes artifact: 7f507bb7c9 user: dgp
04:52 • Ticket [416643ffff]: 4 changes artifact: b8076dadbd user: dgp
04:49 • Ticket [416643ffff]: 4 changes artifact: e1e9c0e968 user: davygrvy
03:07 • Ticket [416643ffff]: 4 changes artifact: 7750a4613e user: dgp
2001-04-17
18:31 • Ticket [416643ffff]: 4 changes artifact: 135e8919d7 user: davygrvy
11:27 • New ticket [416643ffff]. artifact: ce2c4b8ed4 user: davygrvy

rand() has identical results in threads.

Closed / Fixed416643ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2001-04-17 11:27:49
This is having difficulty on windows.  Isn't the c-
runtime supposed to handle this situation just fine?  
I'm perplexed by this:


package require Thread

foreach a {1 2 3 4 5 6} {
   set threads($a) [thread::create { proc doit {} { 
puts [expr {rand()}] }; thread::wait }]
}

foreach a {1 2 3 4 5 6} {
   thread::send -async $threads($a) doit
}

foreach a {1 2 3 4 5 6} {
   thread::send -async $threads($a) thread::exit
}


C:\Program Files\Tcl\bin>tclsh84t
% source test.tcl
% 0.0541875106535
0.0541875106535
0.0541875106535
0.0541875106535
0.0541875106535
0.0541875106535


I really don't think this is a Tcl inherit to Tcl, but 
does show an interesting behavior.  Changing the run 
to be syncronous got this:

package require Thread

foreach a {1 2 3 4 5 6} {
   thread::create { puts [expr {rand()}] }
}

% source test.tcl
0.11639496317
0.366838779471
0.366838779471
0.484234318363
0.609456226513
% 0.726851765405


The docs state "Each interpreter has it's own seed." 
on the expr man page, but I tend to disagree after 
seeing the above.  Where's the seeding happening?  
Should Tcl_CreateInterp() do the seeding?
davygrvyplain2001-04-17 18:31:21
Logged In: YES 
user_id=7549

wait a sec...  2 of those in the syncronous test were 
identical.  This is interesting :)
dgpplain2001-04-18 03:07:37
Logged In: YES 
user_id=80530

Is this a new problem, or does it also exist in Tcl 8.3.2?

Tcl has it's own rand() implementation -- it does not call
a system rand() -- so this problem is a problem with Tcl.

There is one seed per interp: iPtr->randSeed, and it is
initialized in ExprRandFunc() in generic/tclExecute.c.
davygrvyplain2001-04-18 04:49:03
Logged In: YES 
user_id=7549

I haven't checked 8.3.2 or 8.3.3, yet.

It could be possible that all 6 threads hit this bit of 
code at the exact same time (line 4087, tclExecute.c):

iPtr->randSeed = TclpGetClicks();

I leave this up to you guys.  I'm not sure if this is 
really a "bug" or not.  I can send all the threads a seed 
when I start them.  I can get around the behavior in 
script.  I don't know if this warrants a sluething.
dgpplain2001-04-18 04:52:23
Logged In: YES 
user_id=80530

Since I just rewrote rand() for Tcl 8.4a2 and Tcl 8.3.3,
I want to find out whether this is something new I broke
or not.
dgpplain2001-04-18 05:13:28
Logged In: YES 
user_id=80530

Ah.  I see you report this on Windows.  Do you still see
this behavior using the latest CVS HEAD sources (8.4a3)?
Since 8.4a2 was released, Kevin Kenny enhanced TclpGetTime
to get much finer "clicks" resolution on Windows.  That
might take care of this.

If you're really using 8.4a2, that rules out the new
rand() code too, since it was added since then.
davygrvyplain2001-04-18 06:15:59
Logged In: YES 
user_id=7549

Say I do update and rebuild for the HEAD, and all threads 
do hit line 4087, tclExecute.c:

iPtr->randSeed = TclpGetClicks();

and for the sake of discussion, TclpGetClicks() returns the 
exact same result for all threads running in parallel.  
Even with a higher resolution of the timer, the 
*possibility* for a same seed value still exists.

Would it make sense to have the thread extension do this 
for me by pre-seeding so even if parallel threads call rand
() at the exact same moment, the return will be different?
davygrvyplain2001-04-20 08:49:36
Logged In: YES 
user_id=7549

Don,

I'm happy with the attached patch.  These are the results 
for me (on windows) running the first code snippit:

source test2.tcl
% 0.928865028046
0.515779082904
0.648316936404
0.315270977707
0.826455665671
0.931451399779
source test2.tcl
% 0.878650014232
0.370051698
0.808093390804
0.0654099826074
0.547328303357
0.958690000679
source test2.tcl
% 0.210988179879
0.774670734897
0.41666902854
0.185170308307
0.105992445772
0.954533711986
info patch
8.4a3
%

If I remember correctly, Thread IDs on UNIX have smaller 
numbers then on windows and might not effectively shift 
TclpGetClicks deeply enough.  I'll leave the math up 
someone more qualified than myself.
davygrvyplain2001-04-20 10:20:41
File Added - 5549: rand_patch.txt
davygrvyplain2001-04-21 05:36:17
Logged In: YES 
user_id=7549

This seems to work Ok, too.

iPtr->randSeed = TclpGetClicks() ^ ((long) 
Tcl_GetCurrentThread() << ((TclpGetClicks() % 10) + 17));
davygrvyplain2001-04-26 08:01:39
Logged In: YES 
user_id=7549

Don,

I hand this to you.  I can handle this from script anyways, 
so it's no rush.
dgpplain2001-04-30 01:35:18
Logged In: YES 
user_id=80530

The patch looks OK to me.  I think I would add
a more detailed explanation in the comment about
why the Tcl_GetCurrentThread call is there, maybe
even a link back to this bug report.

I'm assigning this to the proper Category for final
maintainer review and commit.
msofer Ticket [416643ffff]: 1 change [History diff]2001-05-05 05:31:09
msoferplain2001-05-08 05:16:08
Logged In: YES 
user_id=148712

Patch committed.
Log in (anonymous is fine) to add a comment.

Attachments: