Autor Beitrag
Lihlu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Mo 07.12.15 12:00 
Hallo,

ich habe 3 Buttons die ihre Postion/größe zufällig ändern sollen.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
             //CubeOne
            Random rnd = new Random();
            int RndSizeBoxY = rnd.Next(10,80);
            int RndSizeBoxX = rnd.Next(10,80);
            Random rnd2 = new Random();
            int RndPositionY = rnd.Next(0409);
            int RndPositionX = rnd.Next(0434);
            CubeOne.Size = new System.Drawing.Size(RndSizeBoxX, RndSizeBoxY);
            CubeOne.Location = new System.Drawing.Point(RndPositionX, RndPositionY);

            //CubeTwo
            Random CubeTwoRnd = new Random();
            int RndSizeBoxY2 = CubeTwoRnd.Next(1080);
            int RndSizeBoxX2 = CubeTwoRnd.Next(1080);
            Random CubeTwo2 = new Random();
            int RndPositionY2 = CubeTwo2.Next(0409);
            int RndPositionX2 = CubeTwo2.Next(0434);
            CubeTwo.Size = new System.Drawing.Size(RndSizeBoxX2, RndSizeBoxY2);
            CubeTwo.Location = new System.Drawing.Point(RndPositionX2, RndPositionY2);

            //CubeThree
            Random random = new Random();
            int RndSizeBoxY3 = random.Next(1080);
            int RndSizeBoxX3 = random.Next(1080);
            Random CubeThree2 = new Random();
            int RndPositionY3 = CubeThree2.Next(0409);
            int RndPositionX3 = CubeThree2.Next(0434);
            CubeThree.Size = new System.Drawing.Size(RndSizeBoxX3, RndSizeBoxY3);
            CubeThree.Location = new System.Drawing.Point(RndPositionX3, RndPositionY3);


Wenn ich die Form ausführe Würfeln aber alle "Randoms" die gleiche Zahl ...
Wieso ?
Müsste nicht "rnd,rnd2,CubeTowRnd,CubeTwo2,random und CubeThree2" alle eine eigene Zahl würfeln ?

Vielen Dank


Moderiert von user profile iconChristian S.: Topic aus WinForms verschoben am Mo 07.12.2015 um 11:24
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 07.12.15 12:16 
Random ist ein Pseudozufallszahlengenerator. Bei gleichen Startbedingungen kommt immer die selber Zahlenserie raus. Du erzeugst immer eine neue Random Instanz über den Standardkonstruktor also bekommst du jedesmal die selbe ~Zufalls~zahlenfolge. Du solltest a) die Random Klasse mit irgendwas initialisieren. Also den Konstruktor benutzen dem man einen Seed mitgeben kann. Ein einfacher Seed wäre z.B. DateTime.Now.Millisecond. Und b) solltest du nicht mehrere Instanzen von Random benutzen sondern nur eine.

Für diesen Beitrag haben gedankt: Lihlu
Lihlu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Mo 07.12.15 12:23 
Alles klar!

Danke - ich werde mal schauen ob ich das gelöst kriege :)
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mo 07.12.15 12:24 
user profile iconRalf Jansen hat folgendes geschrieben Zum zitierten Posting springen:
Du solltest a) die Random Klasse mit irgendwas initialisieren. Also den Konstruktor benutzen dem man einen Seed mitgeben kann. Ein einfacher Seed wäre z.B. DateTime.Now.Millisecond.

Der Standardkonstruktor seedet doch eh schon zeitabhängig, oder? :gruebel:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".

Für diesen Beitrag haben gedankt: Lihlu
Lihlu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Mo 07.12.15 12:28 
Lösung:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
            Random rnd = new Random();
            int RndSizeBoxY = rnd.Next(1080);
            int RndSizeBoxX = rnd.Next(1080);
            int RndSizeBoxY2 = rnd.Next(1080);
            int RndSizeBoxX2 = rnd.Next(1080);
            int RndSizeBoxY3 = rnd.Next(1080);
            int RndSizeBoxX3 = rnd.Next(1080);
            int RndPositionY = rnd.Next(0409);
            int RndPositionX = rnd.Next(0434);
            int RndPositionY2 = rnd.Next(0409);
            int RndPositionX2 = rnd.Next(0434);
            int RndPositionY3 = rnd.Next(0409);
            int RndPositionX3 = rnd.Next(0434);

            CubeOne.Size = new System.Drawing.Size(RndSizeBoxX, RndSizeBoxY);
            CubeOne.Location = new System.Drawing.Point(RndPositionX, RndPositionY);
            CubeTwo.Size = new System.Drawing.Size(RndSizeBoxX2, RndSizeBoxY2);
            CubeTwo.Location = new System.Drawing.Point(RndPositionX2, RndPositionY2);
            CubeThree.Size = new System.Drawing.Size(RndSizeBoxX3, RndSizeBoxY3);
            CubeThree.Location = new System.Drawing.Point(RndPositionX3, RndPositionY3);
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 07.12.15 12:35 
Zitat:
Der Standardkonstruktor seedet doch eh schon zeitabhängig, oder? :gruebel:


Da hast du scheinbar recht. Das hatte ich anders in Erinnerung.