Pairs

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB", "CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen
 
Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB", "CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen


Hi,

I don't understand what do you want to achieve.

Moty
 
Moty Michaely said:
Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB",
"CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen


Hi,

I don't understand what do you want to achieve.

Moty



When I run this script I wil only two output version:
1) AB, CD, BD, AC, BC, AD, AB, CD
2) BA, DC, DB, CA, CB, DA, BA, DC *

All pairs are turnd.

What I want is that it not allways turn all the pairs, but some of them in
random order.
I.e.
x) AB, DC, DB, AC, BC, AD, BA, CD

Do you understand?

Thanks,
Arjen
 
Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB", "CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen


Initialise the Random instances outside of the loop
 
AJ said:
Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB",
"CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen


Initialise the Random instances outside of the loop


Thanks!

Arjen
 
Arjen said:
Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB", "CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen


The problem is that you are creating several Random objects in a short
time. If you don't specify a seed, it uses a time based value, and if
you create several objects in a short time, the time value will not be
different between them, so they will use the same seed value.

The solution is, as AJ suggested, to create a single Random object
outside the loop, and use that object to create all random values.

Also, in your code you are seeding one Random object from another. This
is normally something that it does by itself when creating a sequence of
random numbers, so why are you doing it in your code? Was it just an
experiment?
 
Göran Andersson said:
Arjen said:
Hi,

I have this string:
private string[] pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB",
"CD" };

Now I want to randomize all pairs, like the first AB to BA.

for (int i = 0; i < 8; i++)
{
string pair = pairs;

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.next(8)));
int result = rnd2.Next(2);
int second = 1;

if (result == 1)
{
second = 0;
}

Response.Write(pair[result] + " " + pair[second] + "<br />");
}

The problem is that I allways randomize the whole string. Thus if it AB
changed to BA, CD also will be DC.

What I want is 'true' randomization.

Can someone help?

Thanks!
Arjen


The problem is that you are creating several Random objects in a short
time. If you don't specify a seed, it uses a time based value, and if you
create several objects in a short time, the time value will not be
different between them, so they will use the same seed value.

The solution is, as AJ suggested, to create a single Random object outside
the loop, and use that object to create all random values.

Also, in your code you are seeding one Random object from another. This is
normally something that it does by itself when creating a sequence of
random numbers, so why are you doing it in your code? Was it just an
experiment?



When I start the program with one random object it always will have the same
sequence (as started the last time).
When using two object it does not.

Arjen
 
Arjen said:
When I start the program with one random object it always will have the
same sequence (as started the last time).
When using two object it does not.
No, that doesn't help. If the first Random each time generates the same
sequence, the second Random will each time get the same seed and alllways
generate the same sequence aswell.

The parameterless constructor of Random uses a timedependent seed. So for
any new run of the programm, it will generate a new sequence for every
programm-run. The problem is, that the timedependencie is rather coarse.
Therefore, if this constructor is called several times in a short timeperiod
there is a great chance that all instances generate the same sequence. This
easily happens in a programm like yours but it can't happen betwenn several
programm runs started by the user. The user is to slow for this effect (or
slow enough).

Christof
 
Back
Top