C
C#2y ago
Whiteboy

Why all my randoms are the same? [Answered]

so there is the code and the outputs why are they the same and how to change it?
28 Replies
Whiteboy
Whiteboy2y ago
private static double GetRandomNumber(double minimum, double maximum)
{
Random random = new();
return random.NextDouble() * (maximum - minimum) + minimum;
}
private static double GetRandomNumber(double minimum, double maximum)
{
Random random = new();
return random.NextDouble() * (maximum - minimum) + minimum;
}
public static void CreateInstance()
{
string strSeperator = ";";
StringBuilder sbOutput = new();
_ = new Random();

double x = SharedClass.RarityLvlValue.Rarity * 0.4;

for (var i = 1; i <= 5; i++)
{
RarityBonus["button" + i] = new RarityStats(
armorPen: Math.Round(GetRandomNumber(12, 22), 1) * x,
hitRate: Math.Round(GetRandomNumber(100, 250), 1) * x,
basicDmg: Math.Round(GetRandomNumber(300, 900), 1) * x,
critChance: Math.Round(GetRandomNumber(4, 11), 1) * x,
stunChance: Math.Round(GetRandomNumber(1, 4), 1) * x,
elementalDmg: Math.Round(GetRandomNumber(150, 900), 1) * x,
dmg: Math.Round(GetRandomNumber(8, 18), 1) * x,
highMonster: Math.Round(GetRandomNumber(17, 29), 1) * x,
lowMonster: Math.Round(GetRandomNumber(14, 32), 1) * x);
}
sbOutput.AppendLine(string.Join(strSeperator, RarityBonus));
File.AppendAllText(@"C:\Users\Admin\Desktop\WiT\Semestr 4\Solutions\SimulatorApk\dictRarity.txt", sbOutput.ToString());
}
public static void CreateInstance()
{
string strSeperator = ";";
StringBuilder sbOutput = new();
_ = new Random();

double x = SharedClass.RarityLvlValue.Rarity * 0.4;

for (var i = 1; i <= 5; i++)
{
RarityBonus["button" + i] = new RarityStats(
armorPen: Math.Round(GetRandomNumber(12, 22), 1) * x,
hitRate: Math.Round(GetRandomNumber(100, 250), 1) * x,
basicDmg: Math.Round(GetRandomNumber(300, 900), 1) * x,
critChance: Math.Round(GetRandomNumber(4, 11), 1) * x,
stunChance: Math.Round(GetRandomNumber(1, 4), 1) * x,
elementalDmg: Math.Round(GetRandomNumber(150, 900), 1) * x,
dmg: Math.Round(GetRandomNumber(8, 18), 1) * x,
highMonster: Math.Round(GetRandomNumber(17, 29), 1) * x,
lowMonster: Math.Round(GetRandomNumber(14, 32), 1) * x);
}
sbOutput.AppendLine(string.Join(strSeperator, RarityBonus));
File.AppendAllText(@"C:\Users\Admin\Desktop\WiT\Semestr 4\Solutions\SimulatorApk\dictRarity.txt", sbOutput.ToString());
}
the output
[button1, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button2, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button3, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button4, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button5, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12]
[button1, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button2, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button3, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button4, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12];

[button5, ArmorPenetration: 38.72
BonusHitRate 322.56
BonusBasicDmg: 969.92
BonusCritChance: 12.8
StunChance: 3.2
ElementalDmg: 492.16
BonusDmg: 25.92
HighLvlMonsterDmg: 54.72
LowLvlMonsterDmg: 45.12]
why am i not getting "new" random for every instance?
Kesa
Kesa2y ago
you are, it's just that it's happening so fast that the seed ends up being the same because you are creating a new Random every time. consider keeping around the Random instance and just re-using it instead of newing it up every time.
Susko3
Susko32y ago
make a static Random and use that instead of creating a new one also, due to Math.Round you're not getting an even distribution of values
pox
pox2y ago
Random.Shared
Whiteboy
Whiteboy2y ago
winforms .net framework 4.7.2
TheRanger
TheRanger2y ago
yeah that doesnt happen in .Net core
Whiteboy
Whiteboy2y ago
@TheRanger i have other similar class but on ints not doubles and works just fine
TheRanger
TheRanger2y ago
it seems Random in .Net Framework gives the same seed
Whiteboy
Whiteboy2y ago
[button1, Dmg: 232 - 814
HitRate: 102
CritChance: 13
CritDmg: 304
Rarity: 1
Upgrade Lvl: 0];

[button2, Dmg: 562 - 1942
HitRate: 425
CritChance: 8
CritDmg: 356
Rarity: 7
Upgrade Lvl: 0];

[button3, Dmg: 1380 - 1989
HitRate: 1001
CritChance: 11
CritDmg: 437
Rarity: 0
Upgrade Lvl: 0];

[button4, Dmg: 1751 - 2259
HitRate: 893
CritChance: 16
CritDmg: 423
Rarity: 3
Upgrade Lvl: 0];

[button5, Dmg: 2405 - 3041
HitRate: 554
CritChance: 11
CritDmg: 273
Rarity: 6
Upgrade Lvl: 0]
[button1, Dmg: 232 - 814
HitRate: 102
CritChance: 13
CritDmg: 304
Rarity: 1
Upgrade Lvl: 0];

[button2, Dmg: 562 - 1942
HitRate: 425
CritChance: 8
CritDmg: 356
Rarity: 7
Upgrade Lvl: 0];

[button3, Dmg: 1380 - 1989
HitRate: 1001
CritChance: 11
CritDmg: 437
Rarity: 0
Upgrade Lvl: 0];

[button4, Dmg: 1751 - 2259
HitRate: 893
CritChance: 16
CritDmg: 423
Rarity: 3
Upgrade Lvl: 0];

[button5, Dmg: 2405 - 3041
HitRate: 554
CritChance: 11
CritDmg: 273
Rarity: 6
Upgrade Lvl: 0]
TheRanger
TheRanger2y ago
happens the same with ints but was probably not called so fast which is why you get different numbers
Whiteboy
Whiteboy2y ago
so how do i make static random?
TheRanger
TheRanger2y ago
the same way you define a static method
Whiteboy
Whiteboy2y ago
i can't get it to work
mtreit
mtreit2y ago
Show your attempt
Whiteboy
Whiteboy2y ago
@mtreit i don't know at all
mtreit
mtreit2y ago
What part is not clear?
Whiteboy
Whiteboy2y ago
@mtreit this
mtreit
mtreit2y ago
"this" ?
Whiteboy
Whiteboy2y ago
make a static Random and use that instead of creating a new one
mtreit
mtreit2y ago
Do you know what static means?
Whiteboy
Whiteboy2y ago
private static Random Random()
{
Random random = new();
return random;
}
private static Random Random()
{
Random random = new();
return random;
}
sth like this? i get that there are static members but making a static random?
mtreit
mtreit2y ago
No, make it a static field instead of a method
Whiteboy
Whiteboy2y ago
so like Random random = 0.5?
mtreit
mtreit2y ago
Then have each method that needs random numbers access that field or take the Random instance as a method parameter and pass the static field as a parameter static Random _random = new Random(); As a field of your class that contains the methods Be aware that Random isn't thread safe though
Whiteboy
Whiteboy2y ago
what does that mean D:
mtreit
mtreit2y ago
It means if you are writing multi-threaded code you need to be careful and avoid concurrent access to instances of Random. If you aren't writing multi-threaded / concurrent code right now, don't worry about it.
Whiteboy
Whiteboy2y ago
okay thank you, now it works
Accord
Accord2y ago
✅ This post has been marked as answered!