tags:

views:

114

answers:

4

I want to generate a 10 digit number, and non of its digit occur more than 3 times.

+1  A: 

You are saying number, but integers cant start with zero. So you need to convert generated nunber to string and then add "00" + myGeneratedNumber like this.

Serkan Hekimoglu
This won't work if "myGeneratedNumber" is not 8 characters.
Chris
I didnt give an answer :) Just said, int numbers cant start with 00
Serkan Hekimoglu
@Serkan, I just edited the question :).
SaeedAlg
Ohh sorry, didnt see.
Serkan Hekimoglu
@Serkan Hekimoglu: true. Though your second sentence did suggest that it was the solution to the problem you raised rather than just an example of the kind of thign needing to be done. Might want to rephrase (if one person has misunderstood chances are others would too).
Chris
+2  A: 

The following should do the trick

int rndNumber = new Random().Next();
rndNumber.ToString().PadLeft(10, '0');

It should be noted that you will not get full coverage of the possible range from this (I believe int32 doesn't go high enough) and if you are generating more than one of these then suitably modify your random object to be reused rather than regenerated. If you need to cover the whole range then I'd probably be lazy and just generate two strings of length five using this method (limiting your random number to be five digits max) and put them together.

Edit: Actually, the best way to do this is probably as follows:

        double rndNumber = new Random().NextDouble();
        string testNumber = (rndNumber * 10000000000).ToString("0000000000.000");

Generates a random number between 0 and 1. Multiplies to get a number between 0 and 10000000000 (a 10 digit number at most) and then the ToString formats it correctly to be a 10 digit+ 3 decimial digits.

If you wanted to get rid of trailing zeros then you could do .TrimEnd('0').TrimEnd('.') the TrimEnd('0') will remove trailing zeros (so 0.100 would become 0.1) and the second TrimEnd('.') will remove a trailing '.' in the event the number was a whole number.

If you need full coverage then I would suggest also making a random choice of whether you want the optional section as well and then changing the string format expression that you use. How you weight that choice will depend probably on whether you want to test the with and without equally or if you want to have a balanced spread of all the possibilities.

Chris
@Chris thanks, but I didn't talk about how to generate random number with 10 character its not a problem, problem is how generate a random number which accepts the pattern and its upper bound running time is determined.
SaeedAlg
A random nunmber with 10 characters *will* match the pattern. If you've got that far I'm not sure that your question is well phrased. I've expanded my answer though to give more ideas of how you can do it. Maybe you should put your code in and what it outputs that isn't valid.
Chris
@Chris, for example output like this 4444313225 is not valid, because it has more than 3, occurrence of digit 4. or 55554313226 is not valid and so on.
SaeedAlg
Your regular expression that you've given does nothing to limit the occurences of any given number. Could you please include all relevant information in your question to prevent us answering a different question than the one you want to ask. It seems obvious that the regEx you've given is *not* the one you are actually using to validate.
Chris
I edited it, i just copied from my regular expressions class and i didn't take attention to it. please remove downvote :D
SaeedAlg
I'm not sure that regular expression does what you want either. Maybe you shoudl just explain your criteria in words rather than as a regex? The current one is saying "any 10 digits from 0-9 in a row. Then I can have that repeated up to 3 times." ie you will be getting a 10, 20 or 30 character string made up of any combination of the numbers 0-9. That could include thirty characters all of the same digit. Off the top of my head I can't think of easy ways to prevent repeating of digits in regular expressions. And I'll remove the down vote if the question starts making sense. :)
Chris
thanks currently I'm going to get new down votes :D. i made a mistake, i have a string of 10 digit which has a rule in it no number can occur more than 3 times, so i want to create it. my regular expression knowledge is in low.
SaeedAlg
Bear in mind the purpose of a downvote is as a way of telling you that you aren't asking a good question. If you are not good with regular expressions then don't use them to illustrate a point unless you have tested them. you presumably have logic that does validate them so post that instead or, as you have here, just explain it in words. The original part of your post in which you talk about what you've already tried is good and you should probably put back in - SO users tend to like to see you have made an effort already.
Chris
+1  A: 

I wouldn't use the Random class in C#, rather use RNGCryptoServiceProvider in System.Security.Cryptography, its a much better random number generator, and you specify the length of the random returned.

fARcRY
+1  A: 

New answer since its kind of a different question now...

The following code should do the job you're looking for.

    private string GenerateString()
    {
        Random rnd = new Random();
        List<char> availableNumbers = new List<char>{'0','1','2','3','4','5','6','7','8','9'};
        Dictionary<char, int> counts = new Dictionary<char,int>();
        for (int i = 0; i < 10; i++)
        {
            counts.Add(availableNumbers[i],0);
        }
        char[] generatedCharacters = new char[10];
        for (int i = 0; i < generatedCharacters.Length; i++)
        {
            char digit = availableNumbers[rnd.Next(availableNumbers.Count)];
            generatedCharacters[i] = digit;
            counts[digit]++;
            if (counts[digit] == 3)
                availableNumbers.Remove(digit);
        }
        return new string(generatedCharacters);
    }

Its possible this can be refined and I'm sure LINQ probably does it in one line but I'm not too hot with that. :)

Roughly speaking the idea is that we create a list of available numbers which starts with all the numbers in. We then have a dictionary to keep a count for each character (all 0 initially). For each character in our output string we randomly select a char from our availableNumbers and add it to the string. We then increment the count for that char and if it hits 3 we take it out of the available characters.

This function should not need any trial and error that you were talking about before to find a valid number. However, I'm not 100% sure whether it generates an even spread of outputs though I'm fairly sure it does).

Chris
yes thats it, just should to do this : availableDigits-- after removing number, i think about a one line but its what i want, thanks.
SaeedAlg
mrnye
@mrnye: You are totally right that I didn't need available digits and in fact just wanted to replace it with availableNumbers.Count when generating the random number.And I don't think that the availableNumbers need to be ordered and starting from zero. They are just a list of chars and could be anything really, though I've not run the code with it not in order so I could be wrong... I'll check.
Chris