diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs index 126596818af..e24c49b63f7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs @@ -797,7 +797,7 @@ protected override void BeginProcessing() if (Wait) { - _activityId = (new Random()).Next(); + _activityId = Random.Shared.Next(); if (_timeout == -1 || _timeout >= int.MaxValue / 1000) { _timeoutInMilliseconds = int.MaxValue; diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 3e4c082e101..ddc6fce782d 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -892,7 +892,7 @@ private void FlushPendingOutput() { // System transcripts can have high contention. Do exponential back-off on writing // if needed. - int delay = new Random().Next(10) + 1; + int delay = Random.Shared.Next(10) + 1; bool written = false; while (!written) diff --git a/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs b/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs index 3088667f6b2..dc4850b9208 100644 --- a/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs +++ b/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs @@ -1466,7 +1466,7 @@ internal static void LogScriptBlockCreation(ScriptBlock scriptBlock, bool force) // But split the segments into random sizes (10k + between 0 and 10kb extra) // so that attackers can't creatively force their scripts to span well-known // segments (making simple rules less reliable). - int segmentSize = 10000 + (new Random()).Next(10000); + int segmentSize = 10000 + Random.Shared.Next(10000); int segments = (int)Math.Floor((double)(scriptBlockText.Length / segmentSize)) + 1; int currentLocation = 0; int currentSegmentSize = 0; diff --git a/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs b/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs index 48e44bd3013..21d0609a353 100644 --- a/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs +++ b/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs @@ -210,9 +210,7 @@ internal UpdatableHelpCommandBase(UpdatableHelpCommandType commandType) _exceptions = new Dictionary(); _helpSystem.OnProgressChanged += HandleProgressChanged; - Random rand = new Random(); - - activityId = rand.Next(); + activityId = Random.Shared.Next(); } #endregion