Click here to Skip to main content
15,172,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.Timers;
using System.Windows.Forms;

namespace Vize_Deneme
{
    public partial class Form1 : Form
    {
        private int sayac; 
        public Form1()
        {
            InitializeComponent();
        }

        
        private void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            label1.Text = DateTime.Now.ToLongDateString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            double x = Convert.ToDouble(textBox1.Text);
            double y = Math.Pow(x, 3);
            double z = Math.Pow(x,2);
            label3.Text = Math.Log(y + (z*2) +5,2).ToString();
        }

        private void timer2_Tick(object sender, ElapsedEventArgs e)
        {
           
            sayac++;
            if (sayac % 2 == 0) ;
            {
                Random rnd1 = new Random();
                int rndSayi1 = rnd1.Next(0, 5);
                label4.Text = rndSayi1.ToString();
                
                Random rnd2 = new Random();
                int rndSayi2 = rnd2.Next(0, 5);
                label5.Text = rndSayi2.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer2.Enabled = true;
        }
    }
}


What I have tried:

While a random number should come, there is no random number at the moment


https://ibb.co/C0fCKJ0
https://ibb.co/4N7L8d8
Posted
Updated 28-Apr-21 1:26am

1 solution

Why are you creating new Random objects every time you need a random number? Don't do that. You create a single Random object at the class level, then you can use it where you need a random number throughout the class.
C#
public partial class Form1 : Form
    {
        private Random RNG = new Random();
        private int sayac; 

        public Form1()
        {
            InitializeComponent();
        }
        
        private void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            label1.Text = DateTime.Now.ToLongDateString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            double x = Convert.ToDouble(textBox1.Text);
            double y = Math.Pow(x, 3);
            double z = Math.Pow(x,2);
            label3.Text = Math.Log(y + (z*2) +5,2).ToString();
        }

        private void timer2_Tick(object sender, ElapsedEventArgs e)
        {
           
            sayac++;
            if (sayac % 2 == 0) ;
            {
                int rndSayi1 = RNG.Next(0, 5);
                label4.Text = rndSayi1.ToString();
                
                int rndSayi2 = RNG.Next(0, 5);
                label5.Text = rndSayi2.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer2.Enabled = true;
        }
    }
   
Comments
Richard Deeming 27-Apr-21 4:18am
   
It may be worth pointing out that the Random class is not thread-safe. If you're accessing it from multiple threads, the results will be unpredictable at best - and not in a good way. :)
PIEBALDconsult 27-Apr-21 10:47am
   
Which is best. In my opinion, having one static Random for the entire app is best.
Richard Deeming 27-Apr-21 10:49am
   
As I said, it's not thread-safe. If you have one static instance for the entire app, you would have to lock every access to it to avoid corrupting your process state.

Stephen Toub posted a nice workaround for this problem back in 2009:
Getting random numbers in a thread-safe way | .NET Parallel Programming[^]
Dave Kreskowiak 27-Apr-21 10:57am
   
Without reading the article, I'm going to say wrap a Random in a static class with a few methods to return the correct value type and ranges, and wrap every call to the actual Random methods in locks.

Now I'm off to go read it...
Dave Kreskowiak 27-Apr-21 11:03am
   
I was right, but I also like that "instance per thread" method. I don't have a use for a Random using that method, but other object types, maybe.

I'll have to keep that one in the tool bag.

Nice find.
Dave Kreskowiak 27-Apr-21 10:49am
   
Oh come on! Where's your sense of adventure?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900