Exclude numbers starting with certain three digits in C #

I am currently creating a C # application that generates a random 9 digit number that each passes. I am looking for a way to exclude numbers starting with "666". How can I write instructions to exclude numbers, starting with certain numbers.

Here's a snippet of code just in case it helps.

Random SSN = new Random();
string temp = "";
int num = SSN.Next(100000000, 999999999);
temp = num.ToString();

Thank!

+3
source share
5 answers

Well, you could write:

int num;
do {
   num = SSN.Next(100000000, 999999999);
} while (num >= 666000000 && num < 667000000);

(I also used string comparison, but since we always got exactly 9 digits, we can easily do a numerical comparison.)

+5
source

The easiest way:

Random SSN = new Random();
string temp = "";
do
{
    int num = SSN.Next(100000000, 999999999);
    temp = num.ToString();
} while (temp.StartsWith("666"));

, :

Random SSN = new Random();
int num = SSN.Next(100000000, 998999999);
if (num >= 666000000)
    num += 1000000;
+4
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();
+2
source
Random SSN = new Random();
string temp = "";
do
{
    int num = SSN.Next(100000000, 999999999);
    temp = num.ToString();
} while(num.IndexOf("666") == 0);
0
source

This has complexity O (1).

                string temp = "";
                if (SSN.Next(0,999999999) < 588339221)
                {
                    temp = SSN.Next(100000000, 666000000).ToString();
                }
                else
                {
                    temp = SSN.Next(667000000, 1000000000).ToString();
                }
0
source

All Articles