0

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
}
5
Contributors
18
Replies
19
Views
5 Years
Discussion Span
Last Post by ddanbe
Featured Replies
  • 1
    ddanbe 2,539   5 Years Ago

    Copy the following letter by letter. It contains the code by Momerath and it WORKS! [CODE=C#] 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 <= … Read More

0
// 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;
}
0

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

0

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.

0
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 by AleWin: n/a

0
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?

0

@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 by ddanbe: n/a

0
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

0

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.

0

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]);
            }
1

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();
        }
    }
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.