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

  • Home
  • SEARCH
  • 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 260519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:21:18+00:00 2026-05-11T22:21:18+00:00

All I want is a pragmatic random number generator in C# so I can

  • 0

All I want is a pragmatic random number generator in C# so I can say e.g.

int dummyAge = MathHelpers.GetRandomNumber(20,70);

and have it seem quasi random, e.g. to generate dummy data.

Most stack overflow questions on this topic and on the web get into a philosophical discussions on true randomness which is not what I’m interested at the moment, e.g. I did one in PHP a long time ago which uses milliseconds/sleep which is fine for dummy data, I’m just trying to do this in C# quick.

Does anyone have a quick half-decent C# random number generator based on some time seed, etc. or, how could I change the following code so that it always doesn’t generate the same 5 number in a row?

using System;

namespace TestRandom23874
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10));
            Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10));
            Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10));
            Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10));
            Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10));
            Console.ReadLine();
        }
    }

    public class MathHelpers
    {
        public static int GetRandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
    }
}
  • 1 1 Answer
  • 1 View
  • 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-11T22:21:18+00:00Added an answer on May 11, 2026 at 10:21 pm
    public class MathHelpers
        {
            private static Random random = new Random();
            public static int GetRandomNumber(int min, int max)
            {
                return random.Next(min, max);
            }
        }
    

    This way, you’re not creating a new Random object every time, rather you’re reusing the same one. When you recreate a new one quickly enough, they will yield the same results. If however, you reuse an existing one, you’ll get randomness.

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

Sidebar

Related Questions

My application is shipped to many different customers, who all want to have control
All I want to do is make some RPC calls over sockets. I have
All I want is to display some simple text in my viewController and have
All I want to do is have a JOptionPane inputDialog with a JTextArea instead
All I want to have is a full-screen simple text editor. However, I don't
All I want to do is return the index of the i that is
All I want to do is write hello world to a QGraphicsScene in a
All I want is to get the website URL. Not the URL as taken
All I want is a one pixel black border around my white UILabel text.
All I want to know is if there's an easy way to add lines

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.