-1

I have created an application with a static variable (a random number generated on run time). copied .exe to three folder and ran it. the static value is seems to be shared (same) in all three .

IS that expected?

TSport
  • 21
  • 2
  • So you run 3 exe and all observe changes in the static variable made in only one of the exes? That's *very* hard to believe (sharing of piece of memory is achievable to some extent, but from the question it does not sound you know how to start [implementing that](https://stackoverflow.com/questions/25074011/sharing-a-memory-map-simultaneously-between-processes)) ... – Alexei Levenkov Jan 21 '20 at 06:16
  • for clarification of your question, add some code snippet. thank you. – Hamed Moghadasi Jan 21 '20 at 06:41
  • How did you construct the `Random`? – SteveZ Jan 21 '20 at 06:43
  • in the main method RandomNum = new Random().Next(9999).ToString() +; – TSport Jan 21 '20 at 13:49

3 Answers3

0

I don't think the Memory is shared in this case, unless you somehow did that by accident.

However a new Radom() is the equivalent of saying new Random(DateTime.Now.Millisecond). If you run the 3 programs within the same millisecond (which believe me is not just possible, but likely if do via code), or if you have hard coded new Random(same int every time), your random rolls will be the same across all your programs if you are rolling within the same range ie. 0-5 like die or something.

ForbiddenSoul
  • 138
  • 1
  • 9
0

Yes, it's expected. the static variable will take the value from the "number generator" for the first time, then when this variable has a value it will never take the value from the generator next time, it will return the stored value in it. so it's better to use a static method instead of a variable to generate new values.

note : as not multiple running exe

let's suppose we have this class that has static variable and static method:

 public class StaticTest
        {
            static public int RandomNumber=GenerateRandomNumber();
            static public int GenerateRandomNumber()
            {
                Random rnd = new Random();
                int temp = rnd.Next();
                return temp;
            }
        }

and you can test it by calling it from another class and see the output:

    int TempVariable1 =StaticTest.RandomNumber;

    int TempVariable2= StaticTest.RandomNumber;

    int TempMethod1 = StaticTest.GenerateRandomNumber();

    int TempMethod2 = StaticTest.GenerateRandomNumber();

    System.Console.WriteLine("TempVariable1 = {0}", TempVariable1);
    System.Console.WriteLine("TempVariable2 = {0}", TempVariable2);
    System.Console.WriteLine("TempMethod1 = {0}", TempMethod1);
    System.Console.WriteLine("TempMethod2 = {0}", TempMethod2);

the result will be as following :

        TempVariable1   1936885472  int
        TempVariable2   1936885472  int
        TempMethod1     1887921393  int
        TempMethod2     2129299766  int
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aziz Alzayed
  • 236
  • 5
  • 17
0

I think the Random number generator generating same number. That is the reason I am getting same value. The below code sample I did and the mail I received. I have weekly jobs, but the number is repeating all weeks. :(

Steps to reproduce:

  1. Build the below code in release mode.

  2. Copy to 3 folders bin1, bin2, bin3

  3. Create three task scheduler job for the three solutions.

  4. Select three task jobs together and run together.

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net.Mail;

using System.Text;

using System.Threading;

namespace XXX.HistoryExtractor { public class Test { public static String RandomNum1;

    /// <summary>
    /// Start method for Job
    /// </summary>
    /// <param name="args"></param>
    public static void Main(string[] args)
    {

        RandomNum1 = new Random().Next(9999).ToString() + " ";
        MailHandler2 mh = new MailHandler2();
        mh.SendJobStartUpdate();
    }

}

public class MailHandler2
{
    String RandomNum;

    public MailHandler2()
    {
        //Thread.Sleep(1000);
        RandomNum = new Random().Next(9999).ToString() + " ";
    }

    public void SendJobStartUpdate()
    {
        try
        {

            var mail = new MailMessage();
            mail.To.Add("lijo.john@urmail.com");

            mail.Subject = "Job  Started " + RandomNum + "  -  " + DateTime.Now;
            mail.Body = "<br/>Job  Started...";
            mail.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                try
                {
                    smtp.Send(mail);
                }
                catch (Exception c)
                {
                    throw c;
                }
            }

        }
        catch (FormatException eF)
        {

        }

    }

}

}

See the set of mails I received and random nuber get duplicated.

From Subject Received Size Categories
noreply@mailserver.com Job Started 6887 - 1/22/2020 12:42:55 AM 11:13 41 KB
noreply@mailserver.com Job Started 6887 - 1/22/2020 12:42:55 AM 11:13 40 KB
noreply@mailserver.com Job Started 4936 - 1/22/2020 12:42:55 AM 11:13 41 KB

=====================

From Subject Received Size Categories
noreply@mailserver.com Job Started 7953 - 1/22/2020 12:41:16 AM 11:11 41 KB
noreply@mailserver.com Job Started 6001 - 1/22/2020 12:41:16 AM 11:11 41 KB
noreply@mailserver.com Job Started 7953 - 1/22/2020 12:41:16 AM 11:11 41 KB

=============================== From Subject Received Size Categories
noreply@mailserver.com Job Started 1420 - 1/22/2020 12:41:08 AM 11:11 41 KB
noreply@mailserver.com Job Started 5280 - 1/22/2020 12:41:05 AM 11:11 41 KB
noreply@mailserver.com Job Started 5280 - 1/22/2020 12:41:05 AM 11:11 41 KB

TSport
  • 21
  • 2
  • With this code you will get a random number every time, if and only if, they start at different times. You also have no static variables here so your shared memory theory is out. You output their start times, and we can clearly see that the duplicate numbers have started on the same second as each other. If you simply need a unique string, and it does not need to be an Integer per say, You can use the static function "Guid.NewGuid()". That's what GUIDs are for, random unique identifiers. .ToString() will work fine on them. – ForbiddenSoul Jan 22 '20 at 17:58
  • correct. you will get a random number every time, if and only if, they start at different times - this was the issue. random number was generating same time. – TSport Jan 24 '20 at 06:04