I have the following code:
Thread[] threadArray= new Thread[3];
MyObject[] objectArray = new MyObject[3];
for (int i = 0; i < 3; i++)
{
//Create new HotelSupplier object
objectArray[i] = new MyObject();
//Create array of threads and start a new thread for each HotelSupplier
threadArray[i] = new Thread(new ThreadStart(objectArray[i].run));
//Store the name of the thread
threadArray[i].Name = i.ToString();
//Start the thread
threadArray[i].Start();
}
I creating 3 objects and 3 threads. All of the objects are stored in an array, all of the threads are stored in an array.
The run method in MyObject generates a random number between min and max
Random random = new Random();
double min = 50.00;
double max = 500.00;
double price = random.NextDouble() * (max - min) + min;
Console.WriteLine("GENERATING PRICE: " + price);
My output:
GENERATING PRICE: 101.271334780041
GENERATING PRICE: 101.271334780041
GENERATING PRICE: 101.271334780041
I expect 3 different random numbers instead of just one since each thread I thought would be running on a different object.
What am I doing wrong and what do I not understand about threads?