473,804 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pairs

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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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
May 31 '07 #1
7 2801
On May 31, 2:27 pm, "Arjen" <boah...@hotmai l.comwrote:
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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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

May 31 '07 #2

"Moty Michaely" <Mo*****@gmail. comschreef in bericht
news:11******** *************@q 69g2000hsb.goog legroups.com...
On May 31, 2:27 pm, "Arjen" <boah...@hotmai l.comwrote:
>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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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

May 31 '07 #3
AJ
In article <f3**********@n ews4.zwoll1.ov. home.nl>, bo*****@hotmail .com
says...
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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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

May 31 '07 #4

"AJ" <no***@nowhere. comschreef in bericht
news:MP******** *************** *@news.zen.co.u k...
In article <f3**********@n ews4.zwoll1.ov. home.nl>, bo*****@hotmail .com
says...
>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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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
May 31 '07 #5
Arjen wrote:
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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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
_____
http://www.guffa.com
May 31 '07 #6

"Göran Andersson" <gu***@guffa.co mschreef in bericht
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Arjen wrote:
>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[i];

Random rnd1 = new Random();
Random rnd2 = new Random(rnd1.nex t(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
_____
http://www.guffa.com

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
May 31 '07 #7
"Arjen" <bo*****@hotmai l.comschrieb im Newsbeitrag
news:f3******** **@news3.zwoll1 .ov.home.nl...
>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
_____
http://www.guffa.com


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
May 31 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4721
by: Equis Uno | last post by:
Hi, Assume I'm given a 100k file full of key-value pairs: date,value 26-Feb-04,36.47 25-Feb-04,36.43 24-Feb-04,36.30 23-Feb-04,37.00 20-Feb-04,37.00
4
12672
by: Bill | last post by:
If, for example, I retrieve a connectionstring from a config file using something like: Value = ConfigurationSettings.AppSettings; This will return a string that is semi-colon delimited. If I want, say, to retrieve the password from this string will I need to explicity parse it?
3
6306
by: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
2
1790
by: Evan | last post by:
Is there a simple way to to identify and remove matching pairs from 2 lists? For example: I have a= b=
6
9874
by: Evyn | last post by:
Hi, How do I compare 2 maps for identical keys, and if they have identical keys, check if they have identical values? In either case I want to copy ONLY that key value pair to one of two different maps. I know how to copy the entire map to another: // Copy fset1 to fset3 std::copy(fset1.begin(),fset1.end(),std::inserter(fset3, fset3.begin()));
18
3813
by: Alan Isaac | last post by:
I want to generate sequential pairs from a list. Here is a way:: from itertools import izip, islice for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)): print x12 (Of course the print statement is just illustrative.) What is the fastest way? (Ignore the import time.)
7
2223
by: tmitt | last post by:
Hi, I missed a couple classes due to being sick this past week and I am having trouble getting steered in the right direction for my programming assignment. I'd ask the professor for help but he has a total of four hours during the week for office hours, all of which I have a class. But anyways... Write a C program that produces prime pairs. At the start, user is prompted to input a positive integer less than 100,000 after which the program...
15
2453
by: mcjason | last post by:
I saw something interesting about a grid pair puzzle problem that it looks like a machine when you find each that work out the way it does and say with all the others that work out the way they do together overlapping common pieces but say connected each working out as connected, but together as connected it's connected with the others connected. a whole machine where connected together is a condition of the machine together as...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.