Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:10:34+00:00 2026-05-27T04:10:34+00:00

Hello I am new to WPF and i do have some experience in ASP.NET

  • 0

Hello I am new to WPF and i do have some experience in ASP.NET but this is totally new like, labels I would be doing label.Content instead of label.Text, anyways.. I am trying to do a simple form, where upon clicking the buton it shows 5 different random numbers..

When I debug this code line by line, it does randomize and has a string of different numbers but when i do not debug and run it at once and click the button it shows the same number for all? , not sure why… so ideally i would have

[1] [23] [45] [24] [34]

It gives me that result if I debug and step through but if I do not debug and just run the program i get

[23] [23] [23] [23] [23]

Any help would be much appreciated

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int[] numbers = new int[5];
            StringBuilder sb = new StringBuilder();

            List<int> nums = new List<int>();
            foreach (int i in numbers)
            {
               int rand = RandomNumber(1,59);
               nums.Add(rand);
            }


            string numsList = string.Empty;

            foreach (int items in nums)
            {
                numsList += "[" + items.ToString() + "]";
            }

            lblNumber.Content = numsList.ToString();
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }


    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:10:35+00:00Added an answer on May 27, 2026 at 4:10 am

    You should not create a new Random every time (make it a readonly field instead). The seed may always be the same otherwise when creating new instances shortly one after another. It only works in debug mode since the instances are created more slowly when you step through.

    MSDN:

    By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello I'm new to PHP programming and I migrated from ASP .net to PHP..
I'm fairly new to WPF and I have some problems getting databinding to work
Since this is WPF, it may look like lots of code, but don't be
hello im new to python and have been reading over the documentation and am
Hello.Am new to ios development.What have to do for NSOpenPanel to work in the
Hello I am new at Haskell and i'm having problems trying to get this
I'm new in WPF and I'd like to visualize data from SQL query to
I have a WPF application in VS 2008 with some web service references. For
Hello there and Merry Christmas !!! I am new to WPF and I am
I have a C# .Net 3.5 application that sends a multicast Hello packet to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.