Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. getting same numbers when executing a random number generation method?

getting same numbers when executing a random number generation method?

Scheduled Pinned Locked Moved C#
csharplinqgraphicsiotdata-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    auting82
    wrote on last edited by
    #1

    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++)
            {
    
    Richard DeemingR OriginalGriffO 2 Replies Last reply
    0
    • A auting82

      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++)
              {
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • A auting82

        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++)
                {
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        Richard DeemingR 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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!

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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; or 2 in the GetDigitalValue 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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups