public override void Solve(IOManager io) { var sw = new Stopwatch(); sw.Start(); var rand = new XorShift(); var id = io.ReadInt(); var size = io.ReadInt(); var maxColor = io.ReadInt(); var panels = new int[size][]; for (int row = 0; row < panels.Length; row++) { panels[row] = new int[size]; for (int column = 0; column < panels[row].Length; column++) { panels[row][column] = io.ReadChar() - '0'; } } var queryCount = 9999; io.WriteLine(queryCount); for (int i = 0; i < queryCount; i++) { io.WriteLine($"{rand.Next(size) + 1} {rand.Next(size) + 1} {rand.Next(maxColor) + 1}"); } }
public void Annealing(Stopwatch sw) { var random = new XorShift(); var count = 0; var startTime = sw.ElapsedMilliseconds; var temperature = CalculateTemp(startTime, startTime, TimeLimit); while (true) { if (count++ % 10000 == 0) { temperature = CalculateTemp(sw.ElapsedMilliseconds, startTime, TimeLimit); if (sw.ElapsedMilliseconds >= TimeLimit) { break; } } var cardA = random.Next(_takeOrderInv.Length); var cardB = random.Next(_takeOrderInv.Length); if (cardA == cardB) { continue; } var prev = CalculateLocal(cardA, cardB); Swap(ref _takeOrderInv[cardA], ref _takeOrderInv[cardB]); Swap(ref _takeOrder[_takeOrderInv[cardA]], ref _takeOrder[_takeOrderInv[cardB]]); Swap(ref _compressed[cardA], ref _compressed[cardB]); var next = CalculateLocal(cardA, cardB); // 大きい方が優秀 var diff = prev - next; if (!(diff >= 0 || random.NextDouble() <= Math.Exp(diff / temperature))) { Swap(ref _takeOrderInv[cardA], ref _takeOrderInv[cardB]); Swap(ref _takeOrder[_takeOrderInv[cardA]], ref _takeOrder[_takeOrderInv[cardB]]); Swap(ref _compressed[cardA], ref _compressed[cardB]); } } }
private void ConvolutionMod <T>(int lengthA, int lengthB, ulong seed) where T : struct, IStaticMod { var rand = new XorShift(seed); var a = new StaticModInt <T> [lengthA]; var b = new StaticModInt <T> [lengthB]; var aRaw = new ulong[lengthA]; var bRaw = new ulong[lengthB]; for (int i = 0; i < a.Length; i++) { aRaw[i] = rand.Next(); a[i] = StaticModInt <T> .Raw((int)(aRaw[i] % default(T).Mod)); } for (int i = 0; i < b.Length; i++) { bRaw[i] = rand.Next(); b[i] = StaticModInt <T> .Raw((int)(bRaw[i] % default(T).Mod)); } var expected = new StaticModInt <T> [a.Length + b.Length - 1]; for (int i = 0; i < a.Length; i++) { for (int j = 0; j < b.Length; j++) { expected[i + j] += a[i] * b[j]; } } // 各種オーバーロードについてテスト var actualModInt = AtCoder.Math.Convolution(a, b); var actualModIntSpan = AtCoder.Math.Convolution((ReadOnlySpan <StaticModInt <T> >)a, b); var actualInt = AtCoder.Math.Convolution <T>(a.Select(ai => ai.Value).ToArray(), b.Select(bi => bi.Value).ToArray()); var actualUInt = AtCoder.Math.Convolution <T>(a.Select(ai => (uint)ai.Value).ToArray(), b.Select(bi => (uint)bi.Value).ToArray()); var actualLong = AtCoder.Math.Convolution <T>(a.Select(ai => (long)ai.Value).ToArray(), b.Select(bi => (long)bi.Value).ToArray()); var actualULong = AtCoder.Math.Convolution <T>(aRaw, bRaw); Assert.Equal(expected, actualModInt); Assert.Equal(expected, actualModIntSpan.ToArray()); Assert.Equal(expected.Select(ei => ei.Value), actualInt); Assert.Equal(expected.Select(ei => (uint)ei.Value), actualUInt); Assert.Equal(expected.Select(ei => (long)ei.Value), actualLong); Assert.Equal(expected.Select(ei => (ulong)ei.Value), actualULong); }
/// <summary> /// Sets the player. /// </summary> public void SetPlayer(Player Player, SpellDeck Deck, bool Npc) { if (!Npc) { this.Deck = Deck; this.Player = Player; int SpellCnt = Deck.SpellCount; byte[] Tmp = new byte[SpellCnt]; for (byte I = 0; I < SpellCnt; I++) { Tmp[I] = I; } int N = SpellCnt; while (N > 1) { int K = XorShift.Next(N + 1); byte Value = Tmp[K]; Tmp[K] = Tmp[N]; Tmp[N] = Value; --N; } int Cnt = Math.Min(4, SpellCnt); for (int I = 0; I < Cnt; I++) { this.Hand[I] = Tmp[I]; if (I > 0) { this.Hand[I] -= Tmp[I - 1]; } } for (int I = Cnt; I < SpellCnt; I++) { this.SpellQueue.Add(Tmp[I]); } // TODO : Mike, check this int Idx = 0; for (int I = 0; I < SpellCnt; I++) { Idx += this.Hand[I]; } } }
/// <summary> /// Gets a random spell. /// </summary> internal SpellData GetRandomSpell(XorShift Random, RarityData Data) { int Count = this.Spells[Data.Instance].Count; if (Count > 0) { return(this.Spells[Data.Instance][Random.Next(Count)]); } Logging.Info(this.GetType(), "GetRandomSpell() - No spell found for rarity: " + Data.Name); return(null); }
/// <summary> /// Gets a random spell. /// </summary> public SpellData GetRandomSpell(RarityData Data) { int Count = this.Spells[Data.Instance].Count; if (Count > 0) { return(this.Spells[Data.Instance][XorShift.Next(Count)]); } Logging.Warning(this.GetType(), "GetRandomSpell() - No spell found for rarity: " + Data.Name); return(null); }
/// <summary> /// Shuffles the specified list. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="List">The list.</param> public static void Shuffle <T>(this IList <T> List, XorShift Random) { int c = List.Count; while (c > 1) { c--; int r = Random.Next(c + 1); T Value = List[r]; List[r] = List[c]; List[c] = Value; } }
/// <summary> /// Randomizes the reward. /// </summary> public static Reward RandomizeReward(Chest Chest, Home Home) { TreasureChestData ChestData = Chest.Data; Reward Reward = new Reward(); Reward.Spells = RewardRandomizer.RandomizeSpells(ChestData, Home); int MaxGold = ChestData.MaxGold; int MinGold = ChestData.MinGold; if (MaxGold > 0) { if (MaxGold == MinGold) { Reward.Gold = MaxGold; } else { Reward.Gold = XorShift.Next(MinGold, MaxGold); } } return(Reward); }
void SolveBBB() { int N; int W; int L; int[] array = Array.ConvertAll <string, int>(stringRead().ToString().Split(' '), int.Parse); N = array[0]; W = array[1]; L = array[2]; Pair <long, int>[] pair = new Pair <long, int> [N]; array = Array.ConvertAll <string, int>(stringRead().ToString().Split(' '), int.Parse); for (int i = 0; i < N; i++) { pair[i] = new Pair <long, int>(array[i], i); }//for i Array.Sort(pair); Array.Reverse(pair); XorShift rand = new XorShift(); for (int i = 0; i < 1000; i++) { rand.Next(3); }//for i List <Triplet <long, long, int> > tri = new List <Triplet <long, long, int> >(); //x,y,index for (int i = 0; i < N; i++) { if (i == 0) { tri.Add(new Triplet <long, long, int>(0, 0, pair[i].second)); } else { while (true) { long nx = rand.Next(W); long ny = rand.Next(L); int j = 0; for (; j < tri.Count; j++) { if (Bad(tri[j].first, tri[j].second, nx, ny, array[tri[j].third] + pair[i].first)) { break; } }//for j if (j == tri.Count) { tri.Add(new Triplet <long, long, int>(nx, ny, pair[i].second)); break; } } } }//for i long[] rx = new long[N]; long[] ry = new long[N]; for (int i = 0; i < N; i++) { rx[tri[i].third] = tri[i].first; ry[tri[i].third] = tri[i].second; }//for i StringBuilder res = new StringBuilder(); for (int i = 0; i < N; i++) { if (i != 0) { res.Append(" "); } res.Append(rx[i] + " " + ry[i]); }//for i WriteLine(res); }
public override void WithXorShift() { XorShift.Next(int.MinValue, int.MaxValue); }
/// <param name="random">This parameter will be mutated. Be aware of network-safety!</param> /// <param name="cueStates">This parameter will be mutated. Be aware of network-safety!</param> public int SelectSound(XorShift random, ushort[] cueStates) { switch (type) { case CueType.Parallel: return(0); // <- should be ignored by playback code case CueType.Random: return(random.Next(SoundCount)); case CueType.Serial: return(0); // <- should be ignored by playback code case CueType.Cycle: { Debug.Assert(SoundCount <= 16); // Sound storage is 16 bits! int safeSoundCount = System.Math.Min(16, SoundCount); if (cueStates[id] == 0) { cueStates[id] = (ushort)((1u << safeSoundCount) - 1); // Reset } // Find first bit: for (int i = 0; i < safeSoundCount; i++) { if ((cueStates[id] & (1u << i)) != 0) // If set { cueStates[id] &= (ushort)~(1u << i); // Clear return(i); } } // This should never happen: Debug.Assert(false); return(0); } case CueType.RandomCycle: { Debug.Assert(SoundCount <= 16); // Sound storage is 16 bits! int safeSoundCount = System.Math.Min(16, SoundCount); int bitCount; if (cueStates[id] == 0) { bitCount = safeSoundCount; cueStates[id] = (ushort)((1u << safeSoundCount) - 1); // Reset } else { bitCount = 0; int s = cueStates[id]; do { bitCount += (s & 1); s >>= 1; } while(s != 0); } // Find selected bit: int choice = random.Next(bitCount); for (int i = 0; i < safeSoundCount; i++) { if ((cueStates[id] & (1u << i)) != 0) // If set { if (choice == 0) { cueStates[id] &= (ushort)~(1u << i); // Clear return(i); } choice--; } } // This should never happen: Debug.Assert(false); return(0); } default: throw new ArgumentOutOfRangeException(); } }
public string GetRandomString(TagSet tagSet) { return(Definitions.stringsProvider.GetRandomString(tagSet, GameState.language, random.Next())); }
/// <summary> /// Randomizes spells. /// </summary> internal static List <Spell> RandomizeSpells(TreasureChestData Data, XorShift Random, Home Home) { List <Spell> Spells = new List <Spell>(); if (Data.GuaranteedSpellsData.Length > 0) { for (int i = 0; i < Data.GuaranteedSpellsData.Length; i++) { Spell Spell = RewardRandomizer.CreateSpell(Data.GuaranteedSpellsData[i]); Spell.SetMaterialCount(1); Spells.Add(Spell); } } int RandomSpellCount = Data.GetRandomSpellCount(); SpellSet SpellSet = new SpellSet(Data.ArenaData, null); DataTable RaritiesTable = CSV.Tables.Get(Gamefile.Rarity); int[] CountByRarity = new int[RaritiesTable.Datas.Count]; for (int i = 1; i < RaritiesTable.Datas.Count; i++) { int Chance = Data.GetChanceForRarity((RarityData)RaritiesTable.Datas[i]); if (Chance > 0) { Console.WriteLine("Random spells : " + RandomSpellCount); if (RandomSpellCount > 0) { int Cnt = RandomSpellCount / Chance; int Mod = RandomSpellCount % Chance; int Rnd = Random.Next(Chance); Console.WriteLine("Count : " + Cnt); Console.WriteLine("Mod : " + Mod); Console.WriteLine("Rnd : " + Rnd); Console.WriteLine(); if (Mod > Rnd ^ Rnd == Mod) { CountByRarity[i] = Cnt + 1; RandomSpellCount -= Cnt - 1; Console.WriteLine(i + " : " + Cnt); } } } else { Console.WriteLine("Chance for rarity " + RaritiesTable.Datas[i].Name + " is equals to 0. " + Data.Name); } } return(null); for (int i = 0; i < RaritiesTable.Datas.Count; i++) { int j = 0; int k = 0; while (k >= CountByRarity[i]) { if (j >= 4999) { break; } ++j; Spell Spell = RewardRandomizer.CreateSpell(SpellSet.GetRandomSpell(Random, (RarityData)RaritiesTable.Datas[i])); if (Spell != null) { if (Spells.Contains(Spell)) { continue; } if (!Home.HasSpell(Spell.Data) || j - 1 >= 1000) { Spell.AddMaterialCount(1); } Spells.Add(Spell); ++k; } } } return(Spells); }
public char[] FindSolution() { const int generateLimit = 2000; const int timeLimit = 300000; const double densityLimit = 0.02; var stopWatch = new Stopwatch(); stopWatch.Start(); _items = InitializeItems(); var calculator = new ScoreCalculator(_mazeSize, _starts, _targets, _items); int bestScore; if (CalculateDencity() > densityLimit) // 密ならランダムウォーク、疎ならナルトを作る { _walls = CreateRandom(generateLimit, stopWatch, calculator, out bestScore); } else { _walls = CreateNaruto(); bestScore = calculator.CalculateScore(_walls); } double t0 = bestScore * 0.001; double t1 = bestScore * 0.00001; var startTime = stopWatch.ElapsedMilliseconds; var iteration = 0; while (stopWatch.ElapsedMilliseconds < timeLimit) { iteration++; var time = (double)(stopWatch.ElapsedMilliseconds - startTime) / (timeLimit - startTime); var temperature = Math.Pow(t0, 1.0 - time) * Math.Pow(t1, time); var random = _xorShift.Next(100); if (random < 30) { bestScore = TryRotate(calculator, bestScore, temperature); } else if (random < 95) { bestScore = TrySwap(calculator, bestScore, temperature, time); } else { bestScore = TryFlip(calculator, bestScore, temperature); } } Debug.WriteLine("iteration: " + iteration); return(WallToGrid(_walls)); }
public override void WithXorShift() { XorShift.Next(); }
static void Main(string[] args) { Console.WriteLine("Random Algorithm Timing"); Console.WriteLine("======================="); Console.Write("Calculating pre-instantiated times..."); var preinstantiatedResults = new List <TimingResult>(); var systemRandom = new System.Random(); preinstantiatedResults.Add(RunTest("System.Random", () => systemRandom.Next())); var systemRandomWrapper = new SystemRandomWrapper(); preinstantiatedResults.Add(RunTest("SystemRandomWrapper", () => systemRandomWrapper.Next())); var xorShift = new XorShift(); preinstantiatedResults.Add(RunTest("XorShift", () => xorShift.Next())); var linearCongruential = new LinearCongruential(); preinstantiatedResults.Add(RunTest("LinearCongruential", () => linearCongruential.Next())); var mersenneTwister = new MersenneTwister(); preinstantiatedResults.Add(RunTest("MersenneTwister", () => mersenneTwister.Next())); var motherOfAll = new MotherOfAll(); preinstantiatedResults.Add(RunTest("MotherOfAll", () => motherOfAll.Next())); var ranrotB = new RanrotB(); preinstantiatedResults.Add(RunTest("RanrotB", () => ranrotB.Next())); var wellEquidistributedLongPeriodLinear = new WellEquidistributedLongPeriodLinear(); preinstantiatedResults.Add(RunTest("W.E.L.L.", () => wellEquidistributedLongPeriodLinear.Next())); Console.SetCursorPosition(0, Console.CursorTop); DisplayResults("Results of pre-instantiated random objects:", preinstantiatedResults); Console.Write("Calculating non-instantiated times..."); var noninstantiatedResults = new List <TimingResult>(); noninstantiatedResults.Add(RunTest("System.Random", () => { var random = new System.Random(); random.Next(); })); noninstantiatedResults.Add(RunTest("SystemRandomWrapper", () => { var random = new SystemRandomWrapper(); random.Next(); })); noninstantiatedResults.Add(RunTest("XorShift", () => { var random = new XorShift(); random.Next(); })); noninstantiatedResults.Add(RunTest("LinearCongruential", () => { var random = new LinearCongruential(); random.Next(); })); noninstantiatedResults.Add(RunTest("MersenneTwister", () => { var random = new MersenneTwister(); random.Next(); })); noninstantiatedResults.Add(RunTest("MotherOfAll", () => { var random = new MotherOfAll(); random.Next(); })); noninstantiatedResults.Add(RunTest("RanrotB", () => { var random = new RanrotB(); random.Next(); })); noninstantiatedResults.Add(RunTest("W.E.L.L.", () => { var random = new WellEquidistributedLongPeriodLinear(); random.Next(); })); Console.SetCursorPosition(0, Console.CursorTop); DisplayResults("Results of non-instantiated random objects:", noninstantiatedResults); }
/// <summary> /// Randomizes spells. /// </summary> public static List <Spell> RandomizeSpells(TreasureChestData Data, Home Home) { List <Spell> Spells = new List <Spell>(); if (Data.GuaranteedSpellsData.Length > 0) { for (int I = 0; I < Data.GuaranteedSpellsData.Length; I++) { Spell Spell = RewardRandomizer.CreateSpell(Data.GuaranteedSpellsData[I]); Spell.SetMaterialCount(1); Spells.Add(Spell); } } int RandomSpellCount = Data.RandomSpellCount; SpellSet SpellSet = new SpellSet(Data.ArenaData, null); CsvTable RaritiesTable = CsvFiles.Get(Gamefile.Rarities); int[] CountByRarity = new int[RaritiesTable.Datas.Count]; for (int I = 1; I < CountByRarity.Length; I++) { int Chance = Data.GetChanceForRarity((RarityData)RaritiesTable.Datas[I]); if (Chance > 0) { if (RandomSpellCount > 0) { int Cnt = Data.RandomSpellCount / Chance; CountByRarity[I] = Cnt; RandomSpellCount -= Cnt; if (XorShift.Next(Chance) < Data.RandomSpellCount % Chance) { ++CountByRarity[I]; --RandomSpellCount; } } } } CountByRarity[0] = RandomSpellCount; for (int I = 0; I < CountByRarity.Length; I++) { int J = 0; int K = 0; while (K < CountByRarity[I]) { if (J++ >= 5000) { break; } SpellData RandomSpellData = SpellSet.GetRandomSpell(RaritiesTable.GetWithInstanceId <RarityData>(I)); if (RandomSpellData != null) { Spell Spell = RewardRandomizer.CreateSpell(RandomSpellData); if (Spells.Count < 1) { Spell.AddMaterial(1); Spells.Add(Spell); ++K; } else { Spell Existing = Spells.Find(T => T.Equals(Spell)); if (Existing != null) { if ((Home.HasSpell(RandomSpellData) ^ true) | (J - 1 >= 1000)) { Existing.AddMaterial(1); ++K; } } else { Spell.AddMaterial(1); Spells.Add(Spell); ++K; } } } } } RewardRandomizer.CombineSpells(Spells, Data.DifferentSpellCount, CountByRarity, Home); for (int I = 0; I < Spells.Count; I++) { // TODO : Implement Sort Spells. } return(Spells); }
/// <summary> /// Combines a list of spells. /// </summary> public static void CombineSpells(List <Spell> Spells, int Count, int[] CountByRarity, Home Home) { if (Count > 0) { if (Spells.Count >= 2) { int RndCatchupChance = XorShift.Next(100); int CatchupChance = Globals.ChestCatchupChance; int Multiplier1 = 500 * Spells.Count; int Multiplier2 = 250 * Spells.Count; int Multiplier3 = 1000 * Spells.Count / 3; int I = 0; while (Spells.Count >= Count) { int Rnd = XorShift.Next(Spells.Count); Spell Spell = Spells[Rnd]; SpellData SpellData = Spell.Data; Spell Existing = Home.GetSpellByData(SpellData); ++I; if (Existing != null) { if (Spells.Count >= 2) { int J = 0; int K = 0; Spell Existing2 = null; while (true) { K = (Rnd + ++J) % Spells.Count; Existing2 = Home.GetSpellByData(Spells[K].Data); if (Existing2 != null) { if (Existing != Existing2) { int LvlIdx1 = Existing.LevelIndexIfAllMaterialUsed; int LvlIdx2 = Existing2.LevelIndexIfAllMaterialUsed; if ((I >= Multiplier2) | (RndCatchupChance >= CatchupChance) | (LvlIdx1 <= LvlIdx2)) { if (Existing.Data.RarityData == Existing2.Data.RarityData) { break; } } } } else { if (I >= Multiplier3 || Home.LockedSpellCount > 1) { if (Existing.Data.RarityData == Spells[K].Data.RarityData) { break; } } } if (J >= Spells.Count) { goto End; } } Spell.AddMaterial(Spells[K].Count); Spells.RemoveAt(K); } } End: if (I >= Multiplier1) { break; } } } } }