Click here to Skip to main content
15,886,963 members
Home / Discussions / C#
   

C#

 
QuestionOpen Source Project Pin
programmervb.netc++1-Feb-10 13:06
programmervb.netc++1-Feb-10 13:06 
AnswerRe: Open Source Project Pin
April Fans2-Feb-10 20:18
April Fans2-Feb-10 20:18 
GeneralRe: Open Source Project Pin
programmervb.netc++3-Feb-10 16:17
programmervb.netc++3-Feb-10 16:17 
QuestionSending AT commands to a v.92 USB modem Pin
saeidfarahi1-Feb-10 11:55
saeidfarahi1-Feb-10 11:55 
AnswerRe: Sending AT commands to a v.92 USB modem Pin
Luc Pattyn1-Feb-10 12:10
sitebuilderLuc Pattyn1-Feb-10 12:10 
AnswerRe: Sending AT commands to a v.92 USB modem Pin
PIEBALDconsult1-Feb-10 15:01
mvePIEBALDconsult1-Feb-10 15:01 
GeneralRe: Sending AT commands to a v.92 USB modem Pin
Luc Pattyn1-Feb-10 15:11
sitebuilderLuc Pattyn1-Feb-10 15:11 
Questionrandom in background [modified] Pin
Mark H Bishop1-Feb-10 11:21
Mark H Bishop1-Feb-10 11:21 
I have a simple method that takes a string as input and returns the time (in seconds) it takes to generate a another string that is the same – using System.Random.

private double timeOccurance2(string target)
      {
         string trial = string.Empty;
         double intialTime = DateTime.Now.ToOADate();
         Random rand = new Random();

         while (trial != target)
         {
             StringBuilder b = new StringBuilder();
             char[] ch = new char[target.Length];
             for (int i = 0; i < target.Length; i++)
            {
	    //here I have restricted possible characters to ~0-9
               ch[i] = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * rand.NextDouble() + 48)));
            }

            for (int i = 0; i < target.Length; i++)
            {
               b.Append(ch[i].ToString());
            }

            trial = b.ToString();
         }

         double finalTime = DateTime.Now.ToOADate();
         return 24 * 60 * 60 * (finalTime - intialTime);
      }


If I set target = “123246” and run the method repeatedly by clicking a button over and over, I get a random distribution of times, as expected.

Next, using the same target, I run multiple iterations of the method via BackGroundWorkers:

private void button1_Click(object sender, EventArgs e)
     {
        for (int i = 1; i <= 40; i++)
        {
           BackgroundWorker Computation = new BackgroundWorker();
           Computation.DoWork += new DoWorkEventHandler(Computation_DoWork);
           Computation.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Computation_RunWorkerCompleted);
           Computation.RunWorkerAsync();

     private void Computation_DoWork(object sender, DoWorkEventArgs e)
     {
        e.Result = timeOccurance2("123456").ToString();
     }

     private void Computation_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
     {
        richTextBox1.Text += (string)(e.Result);
        richTextBox1.Text += (char)(10);
     }


The result nearly always has a group of 4 numbers within the results that have nearly identical values. for example 407, 407, 422, 438. (Humm, my computer has 4 logical processors.) The cluster does not always appear at the beginning of the result set.

Why?

modified on Tuesday, February 2, 2010 9:38 AM

AnswerRe: random in background Pin
harold aptroot1-Feb-10 11:39
harold aptroot1-Feb-10 11:39 
AnswerRe: random in background Pin
Abhinav S1-Feb-10 11:43
Abhinav S1-Feb-10 11:43 
GeneralRe: random in background Pin
Mark H Bishop2-Feb-10 4:05
Mark H Bishop2-Feb-10 4:05 
AnswerRe: random in background Pin
Luc Pattyn1-Feb-10 12:11
sitebuilderLuc Pattyn1-Feb-10 12:11 
Questionhow to expose an event in user control Pin
nuttynibbles1-Feb-10 10:37
nuttynibbles1-Feb-10 10:37 
AnswerRe: how to expose an event in user control Pin
#realJSOP1-Feb-10 10:47
mve#realJSOP1-Feb-10 10:47 
AnswerRe: how to expose an event in user control Pin
DaveyM691-Feb-10 12:10
professionalDaveyM691-Feb-10 12:10 
GeneralRe: how to expose an event in user control Pin
nuttynibbles1-Feb-10 16:51
nuttynibbles1-Feb-10 16:51 
QuestionAdding and removing differences from List compared to another list Pin
zagitta1-Feb-10 10:34
zagitta1-Feb-10 10:34 
AnswerRe: Adding and removing differences from List compared to another list Pin
#realJSOP1-Feb-10 10:51
mve#realJSOP1-Feb-10 10:51 
GeneralRe: Adding and removing differences from List compared to another list Pin
zagitta1-Feb-10 11:29
zagitta1-Feb-10 11:29 
GeneralRe: Adding and removing differences from List compared to another list Pin
#realJSOP1-Feb-10 23:20
mve#realJSOP1-Feb-10 23:20 
GeneralRe: Adding and removing differences from List compared to another list Pin
zagitta2-Feb-10 2:55
zagitta2-Feb-10 2:55 
GeneralRe: Adding and removing differences from List compared to another list Pin
Dan Mos2-Feb-10 7:36
Dan Mos2-Feb-10 7:36 
GeneralRe: Adding and removing differences from List compared to another list [modified] Pin
zagitta3-Feb-10 4:37
zagitta3-Feb-10 4:37 
GeneralRe: Adding and removing differences from List compared to another list Pin
Luc Pattyn1-Feb-10 12:15
sitebuilderLuc Pattyn1-Feb-10 12:15 
GeneralRe: Adding and removing differences from List compared to another list Pin
#realJSOP1-Feb-10 23:22
mve#realJSOP1-Feb-10 23:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.