Autor |
Beitrag |
Lihlu
Beiträge: 53
|
Verfasst: Mo 07.12.15 12:00
Hallo,
ich habe 3 Buttons die ihre Postion/größe zufällig ändern sollen.
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:
| Random rnd = new Random(); int RndSizeBoxY = rnd.Next(10,80); int RndSizeBoxX = rnd.Next(10,80); Random rnd2 = new Random(); int RndPositionY = rnd.Next(0, 409); int RndPositionX = rnd.Next(0, 434); CubeOne.Size = new System.Drawing.Size(RndSizeBoxX, RndSizeBoxY); CubeOne.Location = new System.Drawing.Point(RndPositionX, RndPositionY);
Random CubeTwoRnd = new Random(); int RndSizeBoxY2 = CubeTwoRnd.Next(10, 80); int RndSizeBoxX2 = CubeTwoRnd.Next(10, 80); Random CubeTwo2 = new Random(); int RndPositionY2 = CubeTwo2.Next(0, 409); int RndPositionX2 = CubeTwo2.Next(0, 434); CubeTwo.Size = new System.Drawing.Size(RndSizeBoxX2, RndSizeBoxY2); CubeTwo.Location = new System.Drawing.Point(RndPositionX2, RndPositionY2);
Random random = new Random(); int RndSizeBoxY3 = random.Next(10, 80); int RndSizeBoxX3 = random.Next(10, 80); Random CubeThree2 = new Random(); int RndPositionY3 = CubeThree2.Next(0, 409); int RndPositionX3 = CubeThree2.Next(0, 434); 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 Christian S.: Topic aus WinForms verschoben am Mo 07.12.2015 um 11:24
|
|
Ralf Jansen
Beiträge: 4705
Erhaltene Danke: 991
VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
|
Verfasst: 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
Beiträge: 53
|
Verfasst: Mo 07.12.15 12:23
Alles klar!
Danke - ich werde mal schauen ob ich das gelöst kriege
|
|
Christian S.
Beiträge: 20451
Erhaltene Danke: 2264
Win 10
C# (VS 2019)
|
Verfasst: Mo 07.12.15 12:24
_________________ Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Für diesen Beitrag haben gedankt: Lihlu
|
|
Lihlu
Beiträge: 53
|
Verfasst: Mo 07.12.15 12:28
Lösung:
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(10, 80); int RndSizeBoxX = rnd.Next(10, 80); int RndSizeBoxY2 = rnd.Next(10, 80); int RndSizeBoxX2 = rnd.Next(10, 80); int RndSizeBoxY3 = rnd.Next(10, 80); int RndSizeBoxX3 = rnd.Next(10, 80); int RndPositionY = rnd.Next(0, 409); int RndPositionX = rnd.Next(0, 434); int RndPositionY2 = rnd.Next(0, 409); int RndPositionX2 = rnd.Next(0, 434); int RndPositionY3 = rnd.Next(0, 409); int RndPositionX3 = rnd.Next(0, 434);
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
Beiträge: 4705
Erhaltene Danke: 991
VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
|
Verfasst: Mo 07.12.15 12:35
Zitat: | Der Standardkonstruktor seedet doch eh schon zeitabhängig, oder? |
Da hast du scheinbar recht. Das hatte ich anders in Erinnerung.
|
|
|