getting same numbers when executing a random number generation method?
-
I am making a DAQ-Simulator that shall simulate generation of analog (0-1V) and digital 1 or 0 signals. When I call my method sampling () and display the sensor data in my textbox, I always get the same numbers for my Analog sensors. Since this is a Random generation it should have been different numbers every time I click on Sample button :sigh:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace DAQ_Simulator
{
public partial class Form1 : Form
{int counter; int maxSid = 10; //Number of sensors 7analog and 3 digital int maxAI, maxDI; // string sensor\_values; string sTxt; private DateTime datetime; // Create an array of 10 sensor objects Sensor\[\] sObj = new Sensor\[10\]; public Form1() { InitializeComponent(); } private void displaySensorData(object sender, EventArgs e) { } private void groupSampl\_Enter(object sender, EventArgs e) { } private void textSampling\_TextChanged(object sender, EventArgs e) { } private void btnSampling\_Click(object sender, EventArgs e) { sampling(); } private void groupLogg\_Enter(object sender, EventArgs e) { } private void txtLogging\_TextChanged(object sender, EventArgs e) { } private void labelLoggingText\_Click(object sender, EventArgs e) { } private void btnLogging\_Click(object sender, EventArgs e) { } private void labelSensorValues\_Click(object sender, EventArgs e) { } private void textSensorValues\_TextChanged(object sender, EventArgs e) { } private void timer1\_Tick(object sender, EventArgs e) { } private void sampling() { for (counter = 0; counter < maxSid; counter++) { sObj\[counter\] = new Sensor(counter); } for (counter = 0; counter < maxSid; counter++) {
-
I am making a DAQ-Simulator that shall simulate generation of analog (0-1V) and digital 1 or 0 signals. When I call my method sampling () and display the sensor data in my textbox, I always get the same numbers for my Analog sensors. Since this is a Random generation it should have been different numbers every time I click on Sample button :sigh:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace DAQ_Simulator
{
public partial class Form1 : Form
{int counter; int maxSid = 10; //Number of sensors 7analog and 3 digital int maxAI, maxDI; // string sensor\_values; string sTxt; private DateTime datetime; // Create an array of 10 sensor objects Sensor\[\] sObj = new Sensor\[10\]; public Form1() { InitializeComponent(); } private void displaySensorData(object sender, EventArgs e) { } private void groupSampl\_Enter(object sender, EventArgs e) { } private void textSampling\_TextChanged(object sender, EventArgs e) { } private void btnSampling\_Click(object sender, EventArgs e) { sampling(); } private void groupLogg\_Enter(object sender, EventArgs e) { } private void txtLogging\_TextChanged(object sender, EventArgs e) { } private void labelLoggingText\_Click(object sender, EventArgs e) { } private void btnLogging\_Click(object sender, EventArgs e) { } private void labelSensorValues\_Click(object sender, EventArgs e) { } private void textSensorValues\_TextChanged(object sender, EventArgs e) { } private void timer1\_Tick(object sender, EventArgs e) { } private void sampling() { for (counter = 0; counter < maxSid; counter++) { sObj\[counter\] = new Sensor(counter); } for (counter = 0; counter < maxSid; counter++) {
You're specifying the same seed each time, so you'll get the same sequence of pseudo-random numbers each time.
Random class : Retrieve the same sequence of random values[^]:
You can generate the same sequence of random numbers by providing the same seed value to the Random(Int32) constructor.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I am making a DAQ-Simulator that shall simulate generation of analog (0-1V) and digital 1 or 0 signals. When I call my method sampling () and display the sensor data in my textbox, I always get the same numbers for my Analog sensors. Since this is a Random generation it should have been different numbers every time I click on Sample button :sigh:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace DAQ_Simulator
{
public partial class Form1 : Form
{int counter; int maxSid = 10; //Number of sensors 7analog and 3 digital int maxAI, maxDI; // string sensor\_values; string sTxt; private DateTime datetime; // Create an array of 10 sensor objects Sensor\[\] sObj = new Sensor\[10\]; public Form1() { InitializeComponent(); } private void displaySensorData(object sender, EventArgs e) { } private void groupSampl\_Enter(object sender, EventArgs e) { } private void textSampling\_TextChanged(object sender, EventArgs e) { } private void btnSampling\_Click(object sender, EventArgs e) { sampling(); } private void groupLogg\_Enter(object sender, EventArgs e) { } private void txtLogging\_TextChanged(object sender, EventArgs e) { } private void labelLoggingText\_Click(object sender, EventArgs e) { } private void btnLogging\_Click(object sender, EventArgs e) { } private void labelSensorValues\_Click(object sender, EventArgs e) { } private void textSensorValues\_TextChanged(object sender, EventArgs e) { } private void timer1\_Tick(object sender, EventArgs e) { } private void sampling() { for (counter = 0; counter < maxSid; counter++) { sObj\[counter\] = new Sensor(counter); } for (counter = 0; counter < maxSid; counter++) {
Your Random instance is a local variable, which means you create a new one each time you call the GetDigitalValue method. Since the Random constructor begins the sequence with a seed value based on the computer clock, if you create several Random instances close together, it is very likely - particularly with modern fast computers - that the same seed value will be used, and as a result all teh instances will generate the same sequence. Change your Random instance to a
private static
variable, and you will get different results.Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Your Random instance is a local variable, which means you create a new one each time you call the GetDigitalValue method. Since the Random constructor begins the sequence with a seed value based on the computer clock, if you create several Random instances close together, it is very likely - particularly with modern fast computers - that the same seed value will be used, and as a result all teh instances will generate the same sequence. Change your Random instance to a
private static
variable, and you will get different results.Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
OriginalGriff wrote:
Since the Random constructor begins the sequence with a seed value based on the computer clock, ...
Except in the posted code, where each instance is passed a seed value. Either the index of the sensor, in the
Sensor
constructor; or2
in theGetDigitalValue
method. So it's no wonder they always return the same sequence of values. :laugh:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer