KittoKatto
KittoKatto

Reputation: 544

Create 10 Unique numbers and save to array

I am trying to generate 10 unique random numbers and save it to an array.
Here is my code, but there is run-time error, but I dunno how.

Can someone help me please ?

static void Main(string[] args)
    {
        int [] generatedNum = new int[10];
        bool duplicated;
        int tempo;
        Random random = new Random();

        // Create first number
        generatedNum[0] = random.Next(1, 25);      

        for (int i = 1; i < 10; i++)
        {
            tempo = random.Next(0, 25);
            do
            {
                duplicated = false;
                foreach (int x in generatedNum)
                {
                    if (x == tempo)
                        duplicated = true;
                }

                if (duplicated == true)
                tempo = random.Next(0, 25);
            } while (duplicated == true);

            // Save unique number to array
            generatedNum[i] = tempo;

        }

        // To check the number saved
        foreach (int i in generatedNum)
        {
            Console.WriteLine("{0}", generatedNum[i]);
        }

    }

Upvotes: 0

Views: 2751

Answers (7)

Akhil Dev
Akhil Dev

Reputation: 1

/*try this,i used an arraylist to store the digits

using System;
using System.Collections;
public class SamplesArrayList
{

    public static void Main()
    {

        // Creates and initializes a new ArrayList.
       ArrayList values = new ArrayList();
        Random random = new Random();

        values.Add(random.Next(0, 25));

        while (values.Count < 10)
        {
            int newValue;

            do
            {
                newValue = random.Next(0, 25);
            } while (values.Contains(newValue));

            values.Add(newValue);
        }

        PrintValues(values);
    }
    public static void PrintValues(IEnumerable myList)
    {
        foreach (Object obj in myList)
            Console.Write("   {0}", obj);
        Console.WriteLine(myList);
    }

}

Upvotes: 0

MeTitus
MeTitus

Reputation: 3428

    static void Main(string[] args)
    {
        var generatedNum = new List<int>();
        var random = new Random();

        while (generatedNum.Count < 10)
        {
            var tempo = random.Next(0, 25);

            if (generatedNum.Contains(tempo)) continue;
            generatedNum.Add(tempo);
        }

        foreach (var i in generatedNum)
        {
            Console.WriteLine("{0}", i);
        }
    }

And you problem was here no?

    // To check the number saved
    foreach (int i in generatedNum)
    {
        Console.WriteLine("{0}", generatedNum[i]);
    }

Upvotes: 0

Rohit
Rohit

Reputation: 10236

Would this work

If you are allowed to use Linq you can try something like this

You can set the range you want

  Random rnd = new Random();
        var randomNumbers = Enumerable.Range(1, 100)
                                      .Select(x => new { val = x, order = rnd.Next() })
                                      .OrderBy(i => i.order)
                                      .Select(x => x.val)
                                      .Take(10).ToArray();

Upvotes: 0

Carra
Carra

Reputation: 17964

To have x unique items out of an array of y, you need a shuffle bag.

You put your numbers 1-25 in a bag, shuffle it and then take the first 10 items. You will now have 10 random items between 1 & 25.

Upvotes: 1

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

This may helps:

public List<int> GetNumber()
{
    Random random = new Random();
    List<int> values = new List<int>();
    values.Add(random.Next(0, 25));

    while (values.Count < 10)
    {
        int newValue;

        do
        {
            newValue = random.Next(0, 25);
        } while (values.Contains(newValue));

        values.Add(newValue);
    }
    return values;
}

Upvotes: 1

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

Change

 foreach (int i in generatedNum)
        {
            Console.WriteLine("{0}", generatedNum[i]);
        }

to

 for (int j=0;j<generatedNum.Length;j++)
        {
            Console.WriteLine("{0}", generatedNum[j]);
        }

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Your last foreach loop is invalid. i is the value, not index, so you can write it directly:

// To check the number saved
foreach (int i in generatedNum)
{
    Console.WriteLine("{0}", i);
}

Upvotes: 0

Related Questions