Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.57/5 (4 votes)
See more:
How can I create 4-digit Random Numbers? without use Structural Loops and Structural condition (for example Loop for,while,if,...) and without use from Timer
result of this program like this program:

4-digit Produced Must different for example if return 1111 is false. but result 1234 is true.

C#
static void Main(string[] args)
       {
           Random Item = new Random();

           int Num1, Num2, Num3, Num4;
           do
           {
               Num1 = Item.Next(0, 10);
               Num2 = Item.Next(0, 10);
               Num3 = Item.Next(0, 10);
               Num4 = Item.Next(0, 10);

           } while (Num1 == Num2 || Num1 == Num3 || Num1 == Num4 || Num2 == Num3 || Num2 == Num4 || Num3 == Num4);

           Console.WriteLine("{0}{1}{2}{3}", Num1, Num2, Num3, Num4);
           Console.ReadKey();
       }
Posted
Updated 23-Dec-15 4:51am
v5
Comments
Sergey Alexandrovich Kryukov 22-Dec-15 16:03pm    
What's wrong with just reading original MSDN documentation?
—SA
BillWoodruff 23-Dec-15 2:22am    
"without loops" ... that does not make much sense, and you need to clarify what this is about.
Philippe Mori 25-Dec-15 20:22pm    
Having a loop like that might not be very efficient as you would loop a few time with same digits... In that case, this should not be much of a problem since you pick only 4 digits among 10. However, if you would pick 90 distinct 2 digits number, the would be a lot of iteration most of the time. You can do some statistics to have an idea of the average number of iteration... or try the function a few thousand time and count the number of iterations. In most case, it might lead to (almost) infinite loop if the probability of getting a sequence without repeating number is too low...

Please see: https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx[^].

Don't make a common mistake, creating the instance of System.Random several times. Remember, the algorithm pseudo-randon. To randomize of, always use the seed:
https://msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx[^].

As you can see from the code sample shown in the article referenced above, you can use System.DateTime.Now for seeding. To understand why seeding is needed and how pseudo-random algorithm gives you random numbers, please see: https://en.wikipedia.org/wiki/Pseudorandom_number_generator[^].

After you create your instance of System.Random, reuse it for all your needs, just one instance. In your case, all other calls should be to System.Random(int, int):
https://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx[^].

[EDIT]

Warning! The parameter names are really misleading. minValue is really the minimum, but maxValue is not really maximum; the documentation calls it "exclusive upper bound", which means that real maximum is maxValue − 1. :-)
See also the notes in Return value section.

[END EDIT]

Your "without loops" is simply an absurd. Okay, code the calls to Random.Next 100 times to get 100 numbers, but I doubt anyone would be so insane.

—SA
 
Share this answer
 
v4
Comments
[no name] 26-Dec-15 7:57am    
Cool! Very great Background Information, thank you. A 5. Bookmarked, in the hope the question survives :)
Sergey Alexandrovich Kryukov 26-Dec-15 10:44am    
Thank you, Bruno.
—SA
With something like that, you don't have any "visible" loop or condition (assuming, you want to generate a single 4 digits number):

C#
class FourRandomDigits
{
	public static string Run()
	{
	    var generator = new FourRandomDigits();
	    return generator.DoRun();
	}

        private static Random rand = new Random();
	private int[] digits = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	private int remainingCount = 10;
	
	private string DoRun()
	{
	    var builder = new StringBuilder();
	    builder.Append(PickOneDigit());
	    builder.Append(PickOneDigit());
	    builder.Append(PickOneDigit());
	    builder.Append(PickOneDigit());
	    return builder.ToString();
	}
	
	private int PickOneDigit()
	{
	    int index = rand.Next(remainingCount);
	    int result = digits[index];
	    digits[index] = digits[--remainingCount];
	    return result;
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900