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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:56:43+00:00 2026-06-13T17:56:43+00:00

I’ve created a random password generator app (I’m a beginner, just for practicing) and

  • 0

I’ve created a “random password generator” app (I’m a beginner, just for practicing) and somehow at first button click it only fills 5-6-7 elements of my array.

This code might be strange (for generating random characters), I just looked ASCII table and searched for numbers, letters and I’ve put them into random min and max number.

 string[] ch = new string [11];
 Random r = new Random();

 private void button1_Click(object sender, EventArgs e)
 {
    for (int i = 0; i <= 9; i++)
    {
        if (Convert.ToInt64(r.Next()) % 3 == 0)
            ch[i] = Convert.ToString(Convert.ToChar(r.Next(49, 57)));
         else if (Convert.ToInt64(r.Next()) % 3 == 1)
             ch[i] = Convert.ToString(Convert.ToChar(r.Next(65, 89)));
         else if (Convert.ToInt64(r.Next()) % 3 == 2)
             ch[i] = Convert.ToString(Convert.ToChar(r.Next(97, 122)));
     }
     pass.Text = (Convert.ToString(ch[0] + "" + ch[1] + "" + ch[2] + "" + ch[3] + "" + ch[4] + "" + ch[5] + "" + ch[6] + "" + ch[7] + "" + ch[8] + "" + ch[9]));   

To decide to use number (ascii 49-57) small letters (65-89) or bigs (97-122) I just made a random number then (truncating divided?) divided by 3 and result is then 0,1 or 2 then made ifs for them.

Might be stupid but its my own idea. So when I click on the button, at first time it shows only about 5,6,7 elements of arrays. Anybody knows why?

  • 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-13T17:56:45+00:00Added an answer on June 13, 2026 at 5:56 pm

    It’s not only the first time that the code fails to put values in all positions.

    You have this control structure:

    if (Convert.ToInt64(r.Next()) % 3 == 0)
      ...
    else if (Convert.ToInt64(r.Next()) % 3 == 1)
      ...
    else if (Convert.ToInt64(r.Next()) % 3 == 2)
      ...
    

    As you are picking a new random number for each condition, there is a 29.63% chance that neither of them will be true.

    If you watch the result closely, you will see that most of the time there are some characters that don’t change.

    You should pick one random number and check its value:

    switch (r.Next(3)) {
     case 0:
       ch[i] = Convert.ToString(Convert.ToChar(r.Next(49, 57)));
       break;
     case 1:
       ch[i] = Convert.ToString(Convert.ToChar(r.Next(65, 89)));
       break;
     case 2:
       ch[i] = Convert.ToString(Convert.ToChar(r.Next(97, 122)));
       break;
    }
    

    A different approach would be to put all the characters that you want to use in a string, and pick from that:

    string chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz";
    
    ch[i] = chars.Substring(r.Next(chars.Length), 1);
    

    This is especially useful if you want to exclude some characters that are easy to mix up, like 0, O, o, 1 and l, then you just exclude them from the string:

    string chars = "23456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.