Remove excessive references to Util.Rand32()

all usages besides fetching a 32bit random value should use rand.next
remove unnecessary do-while loop for calculating random EVs (always
returns 510 in total)
This commit is contained in:
Kurt
2018-07-04 11:30:43 -07:00
parent 326522c76f
commit 397de97f3c
12 changed files with 34 additions and 38 deletions

View File

@@ -461,7 +461,7 @@ private static void SetRandomIVs(PKM pkm, StringInstruction cmd)
}
if (TryGetHasProperty(pkm, cmd.PropertyName, out var pi))
ReflectUtil.SetValue(pi, pkm, Util.Rand32() & pkm.MaxIV);
ReflectUtil.SetValue(pi, pkm, Util.Rand.Next(pkm.MaxIV + 1));
}
}
}

View File

@@ -494,7 +494,7 @@ public static int GetIV(this PKM pk, int index)
/// <param name="gender">Desired <see cref="PKM.Gender"/>.</param>
public static void SetATKIVGender(this PKM pk, int gender)
{
do { pk.IV_ATK = (int)(Util.Rand32() & pk.MaxIV); }
do { pk.IV_ATK = Util.Rand.Next(pk.MaxIV + 1); }
while (pk.Gender != gender);
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
namespace PKHeX.Core
{

View File

@@ -169,14 +169,14 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
Month = (byte)dt.Month;
Year = (byte)dt.Year;
}
int currentLevel = Level > 0 ? Level : (int)(Util.Rand32() % 100 + 1);
int currentLevel = Level > 0 ? Level : Util.Rand.Next(100) + 1;
var pi = PersonalTable.B2W2.GetFormeEntry(Species, Form);
PK5 pk = new PK5
{
Species = Species,
HeldItem = HeldItem,
Met_Level = currentLevel,
Nature = Nature != 0xFF ? Nature : (int)(Util.Rand32() % 25),
Nature = Nature != 0xFF ? Nature : Util.Rand.Next(25),
Gender = pi.Gender == 255 ? 2 : (Gender != 2 ? Gender : pi.RandomGender),
AltForm = Form,
Version = OriginGame == 0 ? SAV.Game : OriginGame,
@@ -247,7 +247,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
// Dumb way to generate random IVs.
int[] finalIVs = new int[6];
for (int i = 0; i < IVs.Length; i++)
finalIVs[i] = IVs[i] == 0xFF ? (int)(Util.Rand32() & 0x1F) : IVs[i];
finalIVs[i] = IVs[i] == 0xFF ? Util.Rand.Next(pk.MaxIV + 1) : IVs[i];
pk.IVs = finalIVs;
int av = 0;
@@ -260,7 +260,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
break;
case 03: // 0/1
case 04: // 0/1/H
av = (int)(Util.Rand32() % (AbilityType - 1));
av = Util.Rand.Next(AbilityType - 1);
break;
}
pk.HiddenAbility = av == 2;
@@ -273,7 +273,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
pk.PID = Util.Rand32();
// Force Gender
do { pk.PID = (pk.PID & 0xFFFFFF00) | Util.Rand32() & 0xFF; } while (!pk.IsGenderValid());
do { pk.PID = (pk.PID & 0xFFFFFF00) | (uint)Util.Rand.Next(0x100); } while (!pk.IsGenderValid());
// Force Ability
if (av == 1) pk.PID |= 0x10000; else pk.PID &= 0xFFFEFFFF;

View File

@@ -283,7 +283,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
if (!IsPokémon)
return null;
int currentLevel = Level > 0 ? Level : (int)(Util.Rand32()%100 + 1);
int currentLevel = Level > 0 ? Level : Util.Rand.Next(100) + 1;
var pi = PersonalTable.AO.GetFormeEntry(Species, Form);
PK6 pk = new PK6
{
@@ -292,7 +292,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
TID = TID,
SID = SID,
Met_Level = currentLevel,
Nature = Nature != 0xFF ? Nature : (int)(Util.Rand32() % 25),
Nature = Nature != 0xFF ? Nature : Util.Rand.Next(25),
Gender = Gender != 3 ? Gender : pi.RandomGender,
AltForm = Form,
EncryptionConstant = EncryptionConstant != 0 ? EncryptionConstant : Util.Rand32(),
@@ -381,15 +381,15 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
if (ivflag == 0) // Random IVs
{
for (int i = 0; i < 6; i++)
finalIVs[i] = IVs[i] > 31 ? (int)(Util.Rand32() & 0x1F) : IVs[i];
finalIVs[i] = IVs[i] > pk.MaxIV ? Util.Rand.Next(pk.MaxIV + 1) : IVs[i];
}
else // 1/2/3 perfect IVs
{
int IVCount = ivflag - 0xFB;
do { finalIVs[Util.Rand32() % 6] = 31; }
do { finalIVs[Util.Rand.Next(6)] = 31; }
while (finalIVs.Count(r => r == 31) < IVCount);
for (int i = 0; i < 6; i++)
finalIVs[i] = finalIVs[i] == 31 ? 31 : (int)(Util.Rand32() & 0x1F);
finalIVs[i] = finalIVs[i] == 31 ? pk.MaxIV : Util.Rand.Next(pk.MaxIV + 1);
}
pk.IVs = finalIVs;
@@ -403,7 +403,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
break;
case 03: // 0/1
case 04: // 0/1/H
av = (int)(Util.Rand32()%(AbilityType - 1));
av = Util.Rand.Next(AbilityType - 1);
break;
}
pk.Ability = pi.Abilities[av];

View File

@@ -307,7 +307,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
if (!IsPokémon)
return null;
int currentLevel = Level > 0 ? Level : (int)(Util.Rand32()%100 + 1);
int currentLevel = Level > 0 ? Level : Util.Rand.Next(100) + 1;
int metLevel = MetLevel > 0 ? MetLevel : currentLevel;
var pi = PersonalTable.USUM.GetFormeEntry(Species, Form);
PK7 pk = new PK7
@@ -317,7 +317,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
TID = TID,
SID = SID,
Met_Level = metLevel,
Nature = Nature != 0xFF ? Nature : (int)(Util.Rand32() % 25),
Nature = Nature != 0xFF ? Nature : Util.Rand.Next(25),
Gender = Gender != 3 ? Gender : pi.RandomGender,
AltForm = Form,
EncryptionConstant = EncryptionConstant != 0 ? EncryptionConstant : Util.Rand32(),
@@ -397,15 +397,15 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
if (ivflag == 0) // Random IVs
{
for (int i = 0; i < 6; i++)
finalIVs[i] = IVs[i] > 31 ? (int)(Util.Rand32() & 0x1F) : IVs[i];
finalIVs[i] = IVs[i] > 31 ? Util.Rand.Next(pk.MaxIV + 1) : IVs[i];
}
else // 1/2/3 perfect IVs
{
int IVCount = ivflag - 0xFB;
do { finalIVs[Util.Rand32() % 6] = 31; }
do { finalIVs[Util.Rand.Next(6)] = 31; }
while (finalIVs.Count(r => r == 31) < IVCount);
for (int i = 0; i < 6; i++)
finalIVs[i] = finalIVs[i] == 31 ? 31 : (int)(Util.Rand32() & 0x1F);
finalIVs[i] = finalIVs[i] == 31 ? pk.MaxIV : Util.Rand.Next(pk.MaxIV + 1);
}
pk.IVs = finalIVs;
@@ -419,7 +419,7 @@ public override PKM ConvertToPKM(ITrainerInfo SAV)
break;
case 03: // 0/1
case 04: // 0/1/H
av = (int)(Util.Rand32()%(AbilityType - 1));
av = Util.Rand.Next(AbilityType - 1);
break;
}
pk.Ability = pi.Abilities[av];

View File

@@ -406,7 +406,7 @@ public PK7 ConvertToPK7()
// IVs
var new_ivs = new int[6];
int flawless = Species == 151 ? 5 : 3;
for (var i = 0; i < new_ivs.Length; i++) new_ivs[i] = (int)(Util.Rand32() & 31);
for (var i = 0; i < new_ivs.Length; i++) new_ivs[i] = Util.Rand.Next(pk7.MaxIV + 1);
for (var i = 0; i < flawless; i++) new_ivs[i] = 31;
Util.Shuffle(new_ivs);
pk7.IVs = new_ivs;

View File

@@ -424,7 +424,7 @@ public PK7 ConvertToPK7()
var special = Species == 151 || Species == 251;
var new_ivs = new int[6];
int flawless = special ? 5 : 3;
for (var i = 0; i < new_ivs.Length; i++) new_ivs[i] = (int)(Util.Rand32() & 31);
for (var i = 0; i < new_ivs.Length; i++) new_ivs[i] = Util.Rand.Next(pk7.MaxIV + 1);
for (var i = 0; i < flawless; i++) new_ivs[i] = 31;
Util.Shuffle(new_ivs);
pk7.IVs = new_ivs;

View File

@@ -424,7 +424,7 @@ protected static int GetHiddenPowerBitVal(int[] ivs)
sum |= (ivs[i] & 1) << i;
return sum;
}
private int HPVal => GetHiddenPowerBitVal(new[] {IV_HP, IV_ATK, IV_DEF, IV_SPE, IV_SPA, IV_SPD});
private int HPVal => GetHiddenPowerBitVal(IVs);
public virtual int HPPower => Format < 6 ? 40*HPVal/63 + 30 : 60;
public virtual int HPType
{
@@ -560,7 +560,7 @@ public bool InhabitedGeneration(int Generation, int species = -1)
case 4: return 3 <= gen && gen <= 4;
case 5: return 3 <= gen && gen <= 5;
case 6: return 3 <= gen && gen <= 6;
case 7: return VC || 3 <= gen && gen <= 7;
case 7: return 3 <= gen && gen <= 7 || VC;
default:
return false;
}
@@ -830,7 +830,7 @@ public void SetShinySID()
{
if (IsShiny) return;
var xor = TID ^ (PID >> 16) ^ (PID & 0xFFFF);
SID = (int)((xor & 0xFFF8) | (Util.Rand32() & 7));
SID = (int)(xor & 0xFFF8) | Util.Rand.Next(8);
}
/// <summary>
/// Applies a <see cref="PID"/> to the <see cref="PKM"/> according to the specified <see cref="Gender"/>.
@@ -882,7 +882,7 @@ public int[] SetRandomIVs(int? flawless = null)
{
int[] ivs = new int[6];
for (int i = 0; i < 6; i++)
ivs[i] = (int)(Util.Rand32() & MaxIV);
ivs[i] = Util.Rand.Next(MaxIV + 1);
int count = flawless ?? GetFlawlessIVCount();
if (count != 0)
@@ -909,7 +909,7 @@ public int[] SetRandomIVs(int[] template, int? flawless = null)
do
{
for (int i = 0; i < 6; i++)
ivs[i] = template[i] < 0 ? (int) (Util.Rand32() & MaxIV) : template[i];
ivs[i] = template[i] < 0 ? Util.Rand.Next(MaxIV + 1) : template[i];
} while (ivs.Count(z => z == MaxIV) < count);
IVs = ivs;

View File

@@ -290,15 +290,12 @@ public static int[] GetRandomEVs(int generation = Generation)
if (generation > 2)
{
var evs = new int[6];
do
{
evs[0] = (byte)Math.Min(Util.Rand32() % 300, 252); // bias two to get maybe 252
evs[1] = (byte)Math.Min(Util.Rand32() % 300, 252);
evs[2] = (byte)Math.Min(Util.Rand32() % (510 - evs[0] - evs[1]), 252);
evs[3] = (byte)Math.Min(Util.Rand32() % (510 - evs[0] - evs[1] - evs[2]), 252);
evs[4] = (byte)Math.Min(Util.Rand32() % (510 - evs[0] - evs[1] - evs[2] - evs[3]), 252);
evs[5] = (byte)Math.Min(510 - evs[0] - evs[1] - evs[2] - evs[3] - evs[4], 252);
} while (evs.Sum(b => b) > 510); // recalculate random EVs...
evs[0] = (byte)Math.Min(Util.Rand.Next(300), 252); // bias two to get maybe 252
evs[1] = (byte)Math.Min(Util.Rand.Next(300), 252);
evs[2] = (byte)Math.Min(Util.Rand.Next(510 - evs[0] - evs[1]), 252);
evs[3] = (byte)Math.Min(Util.Rand.Next(510 - evs[0] - evs[1] - evs[2]), 252);
evs[4] = (byte)Math.Min(Util.Rand.Next(510 - evs[0] - evs[1] - evs[2] - evs[3]), 252);
evs[5] = (byte)Math.Min(510 - evs[0] - evs[1] - evs[2] - evs[3] - evs[4], 252);
Util.Shuffle(evs);
return evs;
}

View File

@@ -46,7 +46,7 @@ private void B_RandomizeBerries_Click(object sender, EventArgs e)
var plantable = Legal.Pouch_Berry_XY; // 0 index is None, skip with rand
for (int i = 0; i < 90; i++) // amount of plots in the game
{
ushort berry = plantable[Util.Rand32() % (plantable.Length - 1) + 1]; // get random berry item ID from list
ushort berry = plantable[Util.Rand.Next(1, plantable.Length)]; // get random berry item ID from list
BitConverter.GetBytes(berry).CopyTo(tree, 6); // put berry into tree.
tree.CopyTo(SAV.Data, SAV.BerryField + 0x10 * i); // put tree into plot
}

View File

@@ -85,7 +85,7 @@ private void B_All_Click(object sender, EventArgs e)
if (ModifierKeys == Keys.Control)
for (int i = 0; i < PuffCount; i++)
newpuffs[i] = (byte)plus10[Util.Rand32() & 1];
newpuffs[i] = (byte)plus10[Util.Rand.Next(2)];
else
{
for (int i = 0; i < PuffCount; i++)