I have a problem, I need to generate 16 random numbers (1-16) and insert them into 16 vector have tried using a for runs a check every time it fires a number but does not work ..

for ( i = 0; i <= 16; i++)
 {
 int[] v = new int[20];
 number = rand.Next(1, 17);
 vett[i] = number;
// control that checks that the numbers are not equal
}
// Fisher-Yates shuffle, also known as Knuth shuffle
vett[0] = 1;
for (int i = 1; i < vett.Length; i++) {
    j = rand.Next(0, i + 1)
    // because we are dealing with integers, we can use the xor-no-temp swap
    vett[i] ^= vett[j];
    vett[j] ^= vett[i];
    vett[i] ^= vett[j];
    // now shuffle
    vett[j] = i + 1;
}

thanks for the reply.I tried the source but there are equal numbers ..

thanks for the reply.I tried the source but there are equal numbers ..

Not if you did it right, there aren't.

Post your new code.

int j = 0, n;
int[] vett = new int[20];

        for (int i = 1; i < vett.Length; i++) {
    j = rand.Next(0, i + 1)
    // because we are dealing with integers, we can use the xor-no-temp swap
    vett[i] ^= vett[j];
    vett[j] ^= vett[i];
    vett[i] ^= vett[j];
    // now shuffle
    vett[j] = i + 1;
}
}
         textBox1.Text += vett[i] + " ";

Edited 4 Years Ago by AleWin: n/a

int j = 0, n;
int[] vett = new int[20];

        for (int i = 1; i < vett.Length; i++) {
    j = rand.Next(0, i + 1)
    // because we are dealing with integers, we can use the xor-no-temp swap
    vett[i] ^= vett[j];
    vett[j] ^= vett[i];
    vett[i] ^= vett[j];
    // now shuffle
    vett[j] = i + 1;
}
}
         textBox1.Text += vett[i] + " ";

Hello sir I am new here and new in programming too so I doubt why we need to swap if we are just printing just one variable and random.Next can give you appropriate output?

@Momerath
Just wondering, I tried this code

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int j = 0;
            int[] vett = new int[5];
            Random rand = new Random();

            for (int i = 1; i < vett.Length; i++) 
            {
                j = rand.Next(0, i + 1);
                // because we are dealing with integers, we can use the xor-no-temp swap
                vett[i] ^= vett[j];
                vett[j] ^= vett[i];
                vett[i] ^= vett[j];
                // now shuffle
                vett[j] = i + 1;
            }

            Console.Read();
        }
    }
}

several times, it gave random sequences like this one: 5 2 3 0 4 (vett is an array of 5 ints)
Should this not be random sequences of 01234 or 12345 ?
Changing range on line 13 had no effect at all.
If I removed the +1 on line 19 I got a random sequence of 01234
EDIT:
@Momerath again: Momerath your posted code works perfectly (OOPS! I copied the code of the OP to do my testing!)
All by all an interesting experience.:|

Edited 4 Years Ago by ddanbe: n/a

int j = 0;
                 int[] vett = new int[11];j = rand.Next(0, i + 1);
                // because we are dealing with integers, we can use the xor-no-temp swap
                vett[i] += vett[j];
                vett[j] += vett[i];
                vett[i] += vett[j];
                // now shuffle
                vett[j] = i + 1;

I do not understand the utility of the code given that the output is 1 2 3 4
and to do this I could also use:

int j = 0;
            int[] vett = new int[5];
            Random rand = new Random();

            for (int i = 1; i < vett.Length; i++) 
            {
                vett[j] = i;
                Console.WriteLine(" "+vett[j]);
            }

            Console.Read();
        }

but I wish it was the output for example 2 3 1 4 or 4 1 2 3 and not 1 2 3 4

If you are commenting on my post, when I said the output was a random sequence of (the digits) 12345, I meant what I said. At one time the output was 51243, the next time it was 15342 etc.

Make it simple:

Random rand = new Random();
            int[] vett = new int[16];
            for (int i = 0; i < vett.Length; i++)
            {
                vett[i] = rand.Next(1, vett.Length + 1);
                Console.WriteLine(vett[i]);
            }

Copy the following letter by letter.
It contains the code by Momerath and it WORKS!

class Program
    {
        static void Main(string[] args)
        {
            const int arraySize = 10;

            int j = 0;
            int[] vett = new int[arraySize];
            Random rand = new Random();

            for (int k = 1; k <= arraySize; k++)
            {
                vett[0] = 1;
                for (int i = 1; i < vett.Length; i++)
                {
                    j = rand.Next(0, i + 1);
                    // because we are dealing with integers, we can use the xor-no-temp swap
                    vett[i] ^= vett[j];
                    vett[j] ^= vett[i];
                    vett[i] ^= vett[j];
                    // now shuffle
                    vett[j] = i + 1;
                }
                Console.Write("Run no. {0} = ", k);
                for (int i = 0; i < vett.Length; i++)
                {
                    Console.Write(vett[i]); Console.Write(" ");
                }
                Console.WriteLine();
            }

            Console.Read();
        }
    }

Nice.
btw, what exactly does this exclusive-OR assignment operator "^=" with numbers?
I know for boolean values that the result is true if and only if exactly one of its operands is true.

This article has been dead for over six months. Start a new discussion instead.