Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Pairs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arjen - 31 May 2007 12:27 GMT
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.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
Moty Michaely - 31 May 2007 12:39 GMT
> Hi,
>
[quoted text clipped - 29 lines]
> Thanks!
> Arjen

Hi,

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

Moty
Arjen - 31 May 2007 12:46 GMT
>> Hi,
>>
[quoted text clipped - 36 lines]
>
> 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
AJ - 31 May 2007 13:07 GMT
> Hi,
>
[quoted text clipped - 29 lines]
> Thanks!
> Arjen

Initialise the Random instances outside of the loop
Arjen - 31 May 2007 13:15 GMT
>> Hi,
>>
[quoted text clipped - 32 lines]
>
> Initialise the Random instances outside of the loop

Thanks!

Arjen
Göran Andersson - 31 May 2007 13:28 GMT
> Hi,
>
[quoted text clipped - 29 lines]
> 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?

Signature

Göran Andersson
_____
http://www.guffa.com

Arjen - 31 May 2007 13:46 GMT
>> Hi,
>>
[quoted text clipped - 43 lines]
> 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
Christof Nordiek - 31 May 2007 14:07 GMT
>> 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
[quoted text clipped - 4 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.