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 8363981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:19:23+00:00 2026-06-09T12:19:23+00:00

Possible Duplicate: Random number generator not working the way I had planned (C#) I

  • 0

Possible Duplicate:
Random number generator not working the way I had planned (C#)

I have a method which return 9 digit string number , if I call this method it works properly as I expected , But not when I call it in loop statement , Because it generate just one number and repeat it .

Here is the code :

private string GenerateRandomNumber()
        {
            Random r = new Random();
            return r.Next(111111111, 999999999).ToString();
        }
        protected void btnSolo_Click(object sender, EventArgs e)
        {
            Response.Write(GenerateRandomNumber());
            // Every time I call this method , it return 9 digit random number wich is different with the previous call like : 146956595  
        }
        protected void btnBulk_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                string randomNumber = GenerateRandomNumber();
                Response.Write("<br /> " + randomNumber);
                //It  create just one 9 digit number 100 times !
            }
        }
  • 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-06-09T12:19:24+00:00Added an answer on June 9, 2026 at 12:19 pm

    Make Random r = new Random(); be done at the class level, and save r as a class variable. Subsequent calls to Next won’t generate the same “random” values.

    private static Random r = new Random();
    
    private string GenerateRandomNumber()
    {
        return r.Next(111111111, 999999999).ToString();
    }
    
    protected void btnSolo_Click(object sender, EventArgs e)
    {
        Response.Write(GenerateRandomNumber());
    }
    
    protected void btnBulk_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            string randomNumber = GenerateRandomNumber();
            Response.Write("<br /> " + randomNumber);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Why does this Random Number Generator not random? I have this test
Possible Duplicates: Random numbers in C# Random number generator not working the way I
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicate: Non repeating random number array I have a String array with many
Possible Duplicate: Why does it appear that my random number generator isn't random in

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.