Click here to Skip to main content
15,913,323 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi everyone
i need a c# code to display random numbers from two ranges.
it shou be displayed like this.the range1(0,5) range2(6,10) and they must not repeat
range1        range2
1             6
2             7 
3             8
4             9   
5             10


What I have tried:

C#
    Random rand = new Random();
    List<int> randomNumbers = new List<int>();

    int range1;
    int range2;

    for (int i = 0; i < 6; i++)
    {
        do
        {
            range1 = rand.Next(0, 10);

        } while (randomNumbers.Contains(range1));
        randomNumbers.Add(range1);
        Console.WriteLine("{0}", range1);

    }

    Random num = new Random();
    List<int> number = new List<int>();

    for (int j = 0; j < 6; j++)
    {
        do
        {
            range2 = num.Next(11, 20);


        } while (number.Contains(range2));
        number.Add(range2);

        Console.WriteLine("{0}", range2);

    }

    Console.ReadLine();
}
Posted
Updated 12-Jul-17 2:12am
v3
Comments
Prifti Constantine 11-Jul-17 6:32am    
You could create a method that takes two parameters and then make use of the c# random class and generate them completely randomly!

If I were doing this, I would look to create two hash based collections. For both sets of collections, I would create a random number and attempt to add it to the collection - if I can't add it because the number is already present, I would add another. The control you put in place is that the size of each collection is based on the difference between the first and second number, so range1 would be based on the distance between 5 and 0. Now you know how to do it, all you're left to do is code it up (as a hint, a useful hash based collection here would be HashSet<int>).
 
Share this answer
 
Comments
[no name] 11-Jul-17 9:34am    
Sorry I have a question: Why "hash based collections" and not simply two collections with the random numbers?
Thank you in advance.
Pete O'Hanlon 11-Jul-17 10:16am    
Just because they are quick to look up against, so if you had a really, really big range of numbers, the lookup to see if the number is present would be quicker than searching an ordinary list.
[no name] 11-Jul-17 10:23am    
Thank you very much.
The problem is the "not repeat" bit - random numbers are just that: random. So putting a constraint that they should not repeat complicates the issue because you can't use random numbers directly.
What you need to do is set up two List, each containing the range. That's easy to do, you're halfway there already:
C#
List<int> range1 = Enumerable.Range(0, 6).ToList();
List<int> range2 = Enumerable.Range(6, 4).ToList();

Then pull out the numbers according to a random index and remove it from the appropriate list:
C#
Random rand = new Random();
for (int i = range1.Count - 1; i >= 0; i--)
    {
    int randomIndex = rand.Next(range1.Count);
    Console.WriteLine(range1[randomIndex]);
    range1.RemoveAt(randomIndex);
    }
 
Share this answer
 
v2
Comments
Member 13284925 12-Jul-17 8:10am    
thank you so very much guys but i have manage to get what i was looking for here is my code. but i still think it needs a little bit of works. its kinda rough on the edges.

Random rand = new Random();
List<int> Number = new List<int>();
List<int> num = new List<int>();
int range;
int range2;
for (int i = 0; i < 12; i++)
{
range = rand.Next(0, 10);
range2 = rand.Next(11, 21);
if (!Number.Contains(range) && !num.Contains(range2))
{
//range = rand.Next(0, 10);
Number.Add(range);
num.Add(range2);
Console.Write("{0,2} {1,2}", range,range2);
if (i % 1 == 0) Console.WriteLine();
}
}
Console.ReadLine();
}
Quote:
they must not repeat

This make that it is not random, it is shuffle. Just like with cards.

The problem with your program is that it degenerate as it pick values, it is more and more difficult to pick unused ones.

There is another technique to shuffle values.
Say you want to shuffle 52 cards.
- make a list of the 52 values in an array.
- pick a random position between 0 and 51 and swap that position with position 0.
- pick a random position between 1 and 51 and swap that position with position 1.
- pick a random position between 2 and 51 and swap that position with position 2.
- repeat until enough cards are shuffled.
- read the array, it is your result.

Advantage, it don't degenerate, for 52 cards, you need 51 uses of random function.
 
Share this answer
 
thank you so very much guys but i have manage to get what i was looking for here is my code. but i still think it needs a little bit of works. its kinda rough on the edges.

<pre>Random rand = new Random();
            HashSet<int> number1 = new HashSet<int>();
            HashSet<int> number2 = new HashSet<int>();
            HashSet<int> number3 = new HashSet<int>();
            HashSet<int> number4 = new HashSet<int>();

            int range1;
            int range2;
            int range3;
            int range4;
            for (int i = 0; i <= 20; i++)
            {
                do
                {
                    range1 = rand.Next(0, 10);

                } while (number1.Contains(range1));
                number1.Add(range1);

                    do
                    {
                        range2 = rand.Next(11, 21);


                    } while (number2.Contains(range2));
                    number2.Add(range2);
                do
                {
                    range3 = rand.Next(22, 32);

                } while (number3.Contains(range3));
                number3.Add(range3);
                do
                {
                    range4 = rand.Next(33,43);

                } while (number4.Contains(range4));
                number4.Add(range4);

                    Console.WriteLine("{0,4}{1,4}{2,4}{3,4}", range1,range2,range3,range4);
            }
            Console.ReadLine();
        }
 
Share this answer
 
v2

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