C
C#3y 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
WhiteboyOP3y 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
Kesa3y 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
Susko33y 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
pox3y ago
Random.Shared
Whiteboy
WhiteboyOP3y ago
winforms .net framework 4.7.2
TheRanger
TheRanger3y ago
yeah that doesnt happen in .Net core
Whiteboy
WhiteboyOP3y ago
@TheRanger i have other similar class but on ints not doubles and works just fine
TheRanger
TheRanger3y ago
it seems Random in .Net Framework gives the same seed
Whiteboy
WhiteboyOP3y 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
TheRanger3y ago
happens the same with ints but was probably not called so fast which is why you get different numbers
Whiteboy
WhiteboyOP3y ago
so how do i make static random?
TheRanger
TheRanger3y ago
the same way you define a static method
Whiteboy
WhiteboyOP3y ago
i can't get it to work
mtreit
mtreit3y ago
Show your attempt
Whiteboy
WhiteboyOP3y ago
@mtreit i don't know at all
mtreit
mtreit3y ago
What part is not clear?
Whiteboy
WhiteboyOP3y ago
@mtreit this
mtreit
mtreit3y ago
"this" ?
Whiteboy
WhiteboyOP3y ago
make a static Random and use that instead of creating a new one
mtreit
mtreit3y ago
Do you know what static means?
Whiteboy
WhiteboyOP3y 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
mtreit3y ago
No, make it a static field instead of a method
Whiteboy
WhiteboyOP3y ago
so like Random random = 0.5?
mtreit
mtreit3y 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
WhiteboyOP3y ago
what does that mean D:
mtreit
mtreit3y 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
WhiteboyOP3y ago
okay thank you, now it works
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server