I am currently building a C# application that is generating a random 9 digit number each run through. I am looking for a way to exclude numbers starting with "666". How would I go about writing a statement to exclude numbers starting with certain digits.
Here's the code snippet just in case it helps.
Random SSN = new Random();
string temp = "";
int num = SSN.Next(100000000, 999999999);
temp = num.ToString();
Thanks!
Well, you could write:
int num;
do {
num = SSN.Next(100000000, 999999999);
} while (num >= 666000000 && num < 667000000);
(I originally used a string comparison too, but as we've always got exactly 9 digits, we can make a numeric comparison easily.)
The easy way would be:
Random SSN = new Random();
string temp = "";
do
{
int num = SSN.Next(100000000, 999999999);
temp = num.ToString();
} while (temp.StartsWith("666"));
If you’re really into efficiency and want to avoid the string comparison and unbounded loop at all costs, you could use:
Random SSN = new Random();
int num = SSN.Next(100000000, 998999999);
if (num >= 666000000)
num += 1000000;
Random SSN = new Random();
string temp = "";
double d = 0.6; // This will help on of choosing one half
int n1 = SSN.Next(100000000, 666000000);
int n2 = SSN.Next(667000000, 999999999);
int num = SSN.NextDouble() > d ? n1 : n2;
temp = num.ToString();
Random SSN = new Random();
string temp = "";
do
{
int num = SSN.Next(100000000, 999999999);
temp = num.ToString();
} while(num.IndexOf("666") == 0);
This has complexity of O(1).
string temp = "";
if (SSN.Next(0,999999999) < 588339221)
{
temp = SSN.Next(100000000, 666000000).ToString();
}
else
{
temp = SSN.Next(667000000, 1000000000).ToString();
}
Related
I want to create a password list.
So far, it works but my list contains only words like D?t1g3+T%J.
Now I want to create words with more sense.
I have a List with words like Banana, Desk and so on.
How is it possible to change randomly the spelling, add numbers and special characters?
For example:
"Banana" -> "baNana123"
Well, I have just come up with this.
string foo = "banana";
List<string> chars = new List<string>();
Random rnd = new Random();
string newPassword = "";
for (int i = 0; i < foo.Length; i++)
{
chars.Add(foo[i].ToString());
}
foreach (var boo in chars)
{
var randomNum = rnd.Next(0, 2);
Debug.WriteLine(randomNum);
if (randomNum == 0)
{
newPassword = newPassword + boo.ToUpper();
}
else
{
newPassword = newPassword + boo.ToLower();
}
}
Debug.WriteLine(newPassword);
What it does: 1)strips the string, in my case "banana" into individual characters
2) for every character program generates randomly 1, or 0. Base on that I just simply convert to lower, or upper case.
All you have to do is to feed the algorithm with strings. And to the adding numbers at the end you can do something like this I quess.
int howMany = rnd.Next(1, 20); //1 to 19 numbers, you can set that manually
for (int i = 0; i < howMany; i++)
{
newPassword = newPassword + rnd.Next(0, 10);
}
Debug.WriteLine(newPassword);
Outcome last run for me: BanANA6469242684
How to generate an 9 digit ITIN number with 70-88 in the fourth and fifth digit?
The IRS site says that
An Individual Taxpayer Identification Number (ITIN) begins with the number 9 and has a range of **70-88 in the fourth and fifth digit. The range was extended to include 900-70-0000 through 999-88-9999, 900-90-0000 through 999-92-9999 and 900-94-0000 through 999-99-9999.
My test is to validate that an error message is seen when user tries to input a valid ITIN
My code is checking for fourth and fifth digit after getting a random number.
A better way would be to get a random number with 9xx-70-0000 to 9xx-88-9999
int RangeOneBoundaryStart = 70;
int RangeOneBoundaryEnd = 88;
int count = 100;
string InvalidMsg = "Invalid Entry. Individual Tax Identification Numbers are not accepted.";
Random rand = new Random();
for (int i = 1; i <=count; i++ )
{
int Rndm1 = rand.Next(900700000, 999889999);
string num = Rndm1.ToString();
string chcknum = num.Substring(3, 2);
int chcknumint = Int32.Parse(chcknum);
if ( chcknumint >= RangeOneBoundaryStart && Rndm1 < RangeOneBoundaryEnd)
{
_applyPage.ClearField(VerifyElement);
_applyPage.EnterTextWithValue(VerifyElement, num);
_applyPage.Click(ClickAnotherElement);
_TestMethods.ValidateError(ElementToVld, InvalidMsg);
}
}
You could generate 3 different random numbers.
var rnd = new Random();
var rnd3 = rnd.Next(900, 1000); // 900 <= x < 1000
var rnd2 = rnd.Next(70, 89); // 70 <= x < 89
var rnd4 = rnd.Next(0, 10000); // 0 <= x < 10000
and then something like to generate the itin.
var itin = rnd3 + "-" + rnd2 + "-" + rnd4.ToString("0000");
https://dotnetfiddle.net/CpoWAq
This question already has answers here:
Best way to repeat a character in C#
(21 answers)
Closed 5 years ago.
Hi im trying to make it so I can generate a random number 1-35 so for example if the number is 25 it will write out in a string 25 equal signs. How can I do that?
Random r = new Random();
r.next(1, 35);
R's result = 25
string result = 25 equal signs
Class string has a constructor that can do the work for you.
Random r = new Random();
int number = r.next(1, 35);
string result = new string('=', number);
Note also that it should be r.Next() not r.next().
Random r = new Random();
int occurrences = r.Next(1, 35);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < occurrences; i++)
{
sb.Append('=');
}
string output = sb.ToString();
Console.WriteLine(output);
You need a loop to repeat adding = to your result.
Update your code to
Random r = new Random();
int total = r.next(1, 35);
string result = "";
for (int i = 0; i < total; i++)
{
result += "=";
}
I've looked at previous posts and none have the answer that I'm looking for.
I'm new to C# and trying to get this little application to work.
I have a file named "hours.txt" with 30 numbers in it and I want to read the file and output the Average and the Highest Number, but I can't seem to get it to work. I've tried changing the array to a string but I'm just really stuck on how to get this to work. Any help or tips would be greatly appreciated.
int[] hoursArray = new int[30];
StreamReader fileSR = new StreamReader("hours.txt");
int counter = 0;
string line = "";
line = fileSR.ReadLine();
while (line != null)
{
hoursArray[counter] = line;
counter = counter + 1;
line = fileSR.ReadLine();
}
fileSR.Close();
int total = 0;
double average = 0;
for (int index = 0; index < hoursArray.Length; index++)
{
total = total + hoursArray[index];
}
average = (double)total / hoursArray.Length;
int high = hoursArray[0];
for (int index = 1; index < hoursArray.Length; index++)
{
if (hoursArray[index] > high)
{
high = hoursArray[index];
}
}
Console.WriteLine("Highest number is: " + high);
Console.WriteLine("The average is: " + average);
Console.ReadLine();
This is wrong since your are putting a string into a int place.
hoursArray[counter] = line;
Parse string to int first.
hoursArray[counter] = int.Parse(line);
As an alternative method, using LINQ, the code could be replaced with the more concise:
string[] lines = System.IO.File.ReadAllLines("hours.txt");
int[] values = lines.Select((l) => int.Parse(l)).ToArray();
double average = values.Average();
int peak = values.Max();
I want to generate an example of a valid input by Regex pattern. I'm programming with C# .Net . Like this:
//this emthod doesn't exists, its an example of funcionality that I want.
Regex.GenerateInputExample("^[0-9]{15}$");
So, this example gives-me a possible value, like 000000000000000. How to do this?
So, this problem would take some time to solve, since the functionality is not built in. I'll give a general way to solve it:
Using an ascii (or unicode) chart find out the character codes that correspond to the characters you are using for your regex (65 =A, 69 = D, etc)
Create a random function with those bounds. Multiple bounds would take a little more trickery (A-Z =26, 0-9 = 10, so a random number from 0- 35)
Random random = new Random();
int randomNumber = random.Next(65, 70); // this generates a random number including the bounds of 65-69)
char temp = (char)random;
Next you would take the randomly generated characters and add them together into a string.
int lowerBound = 65, upperBound =69;
int length = 6;
char temp;
int randomNumber;
string result= "";
Random rand = new Random();
for (int a = 0; a <= length; a++)
{
randomNumber = rand.Next(lowerBound, upperBound);
temp = (char)randomNumber;
result = result + temp;
} //result is the indirect regex generated string
Indirectly giving you a regex generated string.
The next step is parsing information out of a regex. I've provided a simple case below that will not work for every regex, due to regex complexity.
Regex bob = new Regex("[A-Z]");
int lowerBound = Convert.ToInt32(bob.ToString()[1]);
int upperBound = Convert.ToInt32(bob.ToString()[3]);
int length = 6; //length of the string to be generated
char temp;
int randomNumber;
string result= "";
Random rand = new Random();
for (int a = 0; a <= length; a++)
{
randomNumber = rand.Next(lowerBound, upperBound);
temp = (char)randomNumber;
result = result + temp;
}
( This process could be streamlined into class and utilized etc)