473,785 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading / random number problem

Hi,

My R&D department has asked me to look at threading in a Web Service written
in C#, so I came up with the following code:

using System;
using System.Componen tModel;
using System.Threadin g;
using System.Web.Serv ices;

namespace CWSThreading
{
public class CThreading : System.Web.Serv ices.WebService
{
private double mdThread1;
private double mdThread2;

public CThreading()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeCompo nent();
}

[WebMethod]
public string TestThreading()
{
string strOutput = "";

Thread thread1 = new Thread(new ThreadStart(Ran domNumber1));
thread1.Start() ;
strOutput += "Result from 1st thread: " + mdThread1.ToStr ing() + "\n";

Thread thread2 = new Thread(new ThreadStart(Ran domNumber2));
thread2.Start() ;
strOutput += "Result from 2nd thread: " + mdThread2.ToStr ing() + "\n";

return strOutput;
}

protected void RandomNumber1()
{
System.Random rndNumber = new Random((int)Dat eTime.Now.Ticks );
mdThread1 = rndNumber.NextD ouble();
}

protected void RandomNumber2()
{
System.Random rndNumber = new Random((int)Dat eTime.Now.Ticks );
mdThread2 = rndNumber.NextD ouble();
}
}
}

This is being called by a Windows app to which a web reference has been
added pointing to the above web service. A simple form with a button runs
the following code:

using TestWSThreading .wsThreading;

private void cmdTestWSThread ing_Click(objec t sender, System.EventArg s e)
{
wsThreading.CTh reading wsTestThreading = new CThreading();
string strOutput = wsTestThreading .TestThreading( );
MessageBox.Show (strOutput);
wsTestThreading = null;
}
If I set a breakpoint anywhere in the above code which causes me to step
through it, it always returns different random numbers e.g.:

Result from 1st thread: 0.5133791689357 62
Result from 2nd thread: 0.2027999298660 08

However, if I remove the breakpoint so that the web service runs "normally",
I always get the following result:

Result from 1st thread: 0
Result from 2nd thread: 0

Any assistance gratefully received.

Best regards,

Mark Rae
Nov 15 '05
11 1841
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
That's not changed the seed at all - that's just divided the *result*
by the hash code.
Correct.
Instead, it would be better to have *one* random number generator, and
serialize access to it. You only need to have one seed then, and using
the current time for that is reasonable.


Why would this be better?
Nov 15 '05 #11
Mark Rae <ma**@markrae.c o.uk> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
That's not changed the seed at all - that's just divided the *result*
by the hash code.


Correct.
Instead, it would be better to have *one* random number generator, and
serialize access to it. You only need to have one seed then, and using
the current time for that is reasonable.


Why would this be better?


Well, *anything* would be better than just dividing by the hash.
However, seeding the random number generator from something other than
the thread hash would be good, as the thread hash is relatively
predictable.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12

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

Similar topics

10
2506
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
10
2911
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there...
3
1475
by: Manuel | last post by:
I've playing around with multi-threading applications lately. The main problem I have is that when I create a file/table/whatever I need a unique name. The only way I could come up with, is asking for a temporary file (System.IO.Path.GetTempFileName) and that file name would be the unique character string I was looking for. The same goes for the log report files. I have to use the temp file name as seed for the random, wait that amount...
40
2825
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't conceptually understand very well. Here it is: Π – the ratio of the circumference of a circle to its diameter – is one of the most common and important constants in mathematics. It is an irrational number (a real number that cannot be...
10
2187
by: Janto Dreijer | last post by:
I have been having problems with the Python 2.4 and 2.5 interpreters on both Linux and Windows crashing on me. Unfortunately it's rather complex code and difficult to pin down the source. So I've been trying to reduce the code. In the process it's started to crash in different ways. I'm not sure if any of it is related. The following is only crashing Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32 in two different(?) ways.
9
1864
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this CPU-intensive work in another thread, but of course this won't work because of Python's GIL. There's a lot of past discussion on this, and I want to bring it up again because with the work on Python 3000, I think it is worth trying
2
966
by: JonathanB | last post by:
I have a multi-access problem that I'm pretty sure needs to be solved with threading, but I'm not sure how to do it. This will be my first foray into threading, so I'm a little confused by all of the new landscape. So, I'm going to lay out the problem I'm facing and if someone could point me towards a good example of what I need to do, that would be great. I read THE tutorial, and while it made since to me in an esoteric sense, I'm not...
13
2815
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
126
6750
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10101
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
8988
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
7509
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
6744
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();...
1
4063
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
3665
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.