KLM
KLM

Reputation: 25

Random not getting its max value

I am trying to create a console application which is basically a very simple version of a security measure used by some websites and banks etc.

The user will have declared a "Secret Word"(stored in a stringBuilder), after which, every time the user tries to access the program/website etc they have to enter character X of the word (X generated via a random). However, the random never selects the last character of the word.

Example: When i use a 2 letter Secret word, it will only ever select the first letter, when i have tried getting it to select the letter by other methods it thinks that there is a letter 0 which throws the whole application.

Why is the final character never being selected? please help (new to c#)

Code Below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

       namespace randomCharSelecter
       {
            class Program
           {
                static void Main(string[] args)
                {
                    StringBuilder stringBuilder = new StringBuilder();

                    Console.WriteLine("Please Enter Your Secret Word");
                    stringBuilder.Append(Console.ReadLine());           


                    EnterInput:
                    Random rndSelect = new Random();
                    int randomChar1 = rndSelect.Next(1, (stringBuilder.Length));

                    Console.WriteLine("Please enter character {0} of your secret word", randomChar1);
                    string userInput = Console.ReadLine();

                    if (userInput == (Convert.ToString(stringBuilder).Substring(randomChar1 - 1, 1)))
                        Console.WriteLine("Correct!");
                    else
                    {
                        Console.WriteLine("Incorrect! Please try again");
                        goto EnterInput;
                    }

                    Console.ReadLine();
                }
            }
        }

Upvotes: 0

Views: 343

Answers (2)

omni
omni

Reputation: 588

Your problem is that the second value of Random.Next(int val1, int val2) is end-exclusive. So you'll end up having to do stringBuilder.Length + 1

Source: http://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

This is the expected behavior. From the documentation:

Return Value
Type: System.Int32

A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.

In other words if you want a random integer between 1 and stringBuilder.Length, inclusive (that is, that the result could possibly equal stringBuilder.Length), you need to use:

int randomChar1 = rndSelect.Next(1, stringBuilder.Length + 1);

Alternatively, you could simply modify the code to make use of zero-based indexing:

int randomChar1 = rndSelect.Next(0, stringBuilder.Length);
...
if (userInput == (Convert.ToString(stringBuilder).Substring(randomChar1, 1)))
    Console.WriteLine("Correct!");

Upvotes: 2

Related Questions