Skip to content

Commit 706ef73

Browse files
eerhardtTomFinley
authored andcommitted
Remove IRandom and replace with System.Random. (#1786)
* Remove IRandom and replace with System.Random. * Make TauswortheHybrid internal/BestFriend. * Add NextSingle and NextSigned back. * Override Next(int, int) and NextBytes since they are public. * Change Single and Double to float and double.
1 parent 8022c4f commit 706ef73

File tree

71 files changed

+286
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+286
-449
lines changed

src/Microsoft.ML.Api/DataViewConstructionUtils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ protected DataViewBase(IHostEnvironment env, string name, InternalSchemaDefiniti
400400

401401
public abstract long? GetRowCount();
402402

403-
public abstract IRowCursor GetRowCursor(Func<int, bool> predicate, IRandom rand = null);
403+
public abstract IRowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null);
404404

405405
public IRowCursor[] GetRowCursorSet(out IRowCursorConsolidator consolidator, Func<int, bool> predicate,
406-
int n, IRandom rand = null)
406+
int n, Random rand = null)
407407
{
408408
consolidator = null;
409409
return new[] { GetRowCursor(predicate, rand) };
@@ -561,7 +561,7 @@ public override bool CanShuffle
561561
return _data.Count;
562562
}
563563

564-
public override IRowCursor GetRowCursor(Func<int, bool> predicate, IRandom rand = null)
564+
public override IRowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null)
565565
{
566566
Host.CheckValue(predicate, nameof(predicate));
567567
return new Cursor(Host, "ListDataView", this, predicate, rand);
@@ -578,7 +578,7 @@ private int Index
578578
}
579579

580580
public Cursor(IHostEnvironment env, string name, ListDataView<TRow> dataView,
581-
Func<int, bool> predicate, IRandom rand)
581+
Func<int, bool> predicate, Random rand)
582582
: base(env, dataView, predicate)
583583
{
584584
Ch.AssertValueOrNull(rand);
@@ -660,7 +660,7 @@ public override bool CanShuffle
660660
return (_data as ICollection<TRow>)?.Count;
661661
}
662662

663-
public override IRowCursor GetRowCursor(Func<int, bool> predicate, IRandom rand = null)
663+
public override IRowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null)
664664
{
665665
return new Cursor(Host, this, predicate);
666666
}
@@ -747,7 +747,7 @@ public void SetCurrentRowObject(TRow value)
747747
_current = value;
748748
}
749749

750-
public override IRowCursor GetRowCursor(Func<int, bool> predicate, IRandom rand = null)
750+
public override IRowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null)
751751
{
752752
Contracts.Assert(_current != null, "The current object must be set prior to cursoring");
753753
return new Cursor(Host, this, predicate);

src/Microsoft.ML.Api/StatefulFilterTransform.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private StatefulFilterTransform(IHostEnvironment env, StatefulFilterTransform<TS
106106
return null;
107107
}
108108

109-
public IRowCursor GetRowCursor(Func<int, bool> predicate, IRandom rand = null)
109+
public IRowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null)
110110
{
111111
Host.CheckValue(predicate, nameof(predicate));
112112
Host.CheckValueOrNull(rand);
@@ -118,7 +118,7 @@ public IRowCursor GetRowCursor(Func<int, bool> predicate, IRandom rand = null)
118118
return new Cursor(this, input, predicate);
119119
}
120120

121-
public IRowCursor[] GetRowCursorSet(out IRowCursorConsolidator consolidator, Func<int, bool> predicate, int n, IRandom rand = null)
121+
public IRowCursor[] GetRowCursorSet(out IRowCursorConsolidator consolidator, Func<int, bool> predicate, int n, Random rand = null)
122122
{
123123
Contracts.CheckValue(predicate, nameof(predicate));
124124
Contracts.CheckParam(n >= 0, nameof(n));

src/Microsoft.ML.Api/TypedCursor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public IRowCursor<TRow> GetCursor(Func<int, bool> additionalColumnsPredicate, in
192192
{
193193
_host.CheckValue(additionalColumnsPredicate, nameof(additionalColumnsPredicate));
194194

195-
IRandom rand = randomSeed.HasValue ? RandomUtils.Create(randomSeed.Value) : null;
195+
Random rand = randomSeed.HasValue ? RandomUtils.Create(randomSeed.Value) : null;
196196

197197
var cursor = _data.GetRowCursor(GetDependencies(additionalColumnsPredicate), rand);
198198
return new TypedCursor(this, cursor);
@@ -212,7 +212,7 @@ public Func<int, bool> GetDependencies(Func<int, bool> additionalColumnsPredicat
212212
/// <param name="n">Number of cursors to create</param>
213213
/// <param name="rand">Random generator to use</param>
214214
public IRowCursor<TRow>[] GetCursorSet(out IRowCursorConsolidator consolidator,
215-
Func<int, bool> additionalColumnsPredicate, int n, IRandom rand)
215+
Func<int, bool> additionalColumnsPredicate, int n, Random rand)
216216
{
217217
_host.CheckValue(additionalColumnsPredicate, nameof(additionalColumnsPredicate));
218218
_host.CheckValueOrNull(rand);

src/Microsoft.ML.Core/Data/IDataView.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public interface IDataView : ISchematized
9999
/// a getter for an inactive columns will throw. The <paramref name="needCol"/> predicate must be
100100
/// non-null. To activate all columns, pass "col => true".
101101
/// </summary>
102-
IRowCursor GetRowCursor(Func<int, bool> needCol, IRandom rand = null);
102+
IRowCursor GetRowCursor(Func<int, bool> needCol, Random rand = null);
103103

104104
/// <summary>
105105
/// This constructs a set of parallel batch cursors. The value n is a recommended limit
@@ -109,7 +109,7 @@ public interface IDataView : ISchematized
109109
/// an implementation can return a different number of cursors.
110110
///
111111
/// The cursors should return the same data as returned through
112-
/// <see cref="GetRowCursor(Func{int, bool}, IRandom)"/>, except partitioned: no two cursors
112+
/// <see cref="GetRowCursor(Func{int, bool}, Random)"/>, except partitioned: no two cursors
113113
/// should return the "same" row as would have been returned through the regular serial cursor,
114114
/// but all rows should be returned by exactly one of the cursors returned from this cursor.
115115
/// The cursors can have their values reconciled downstream through the use of the
@@ -123,7 +123,7 @@ public interface IDataView : ISchematized
123123
/// <param name="rand">An instance </param>
124124
/// <returns></returns>
125125
IRowCursor[] GetRowCursorSet(out IRowCursorConsolidator consolidator,
126-
Func<int, bool> needCol, int n, IRandom rand = null);
126+
Func<int, bool> needCol, int n, Random rand = null);
127127
}
128128

129129
/// <summary>

src/Microsoft.ML.Core/Data/IHostEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public interface IHost : IHostEnvironment
9494
/// The random number generator issued to this component. Note that random number
9595
/// generators are NOT thread safe.
9696
/// </summary>
97-
IRandom Rand { get; }
97+
Random Rand { get; }
9898

9999
/// <summary>
100100
/// Signal to stop exection in this host and all its children.

src/Microsoft.ML.Core/Environment/ConsoleEnvironment.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public ConsoleEnvironment(int? seed = null, bool verbose = false,
368368
/// <param name="conc">Concurrency level. Set to 1 to run single-threaded. Set to 0 to pick automatically.</param>
369369
/// <param name="outWriter">Text writer to print normal messages to.</param>
370370
/// <param name="errWriter">Text writer to print error messages to.</param>
371-
private ConsoleEnvironment(IRandom rand, bool verbose = false,
371+
private ConsoleEnvironment(Random rand, bool verbose = false,
372372
MessageSensitivity sensitivity = MessageSensitivity.All, int conc = 0,
373373
TextWriter outWriter = null, TextWriter errWriter = null)
374374
: base(rand, verbose, conc, nameof(ConsoleEnvironment))
@@ -401,7 +401,7 @@ protected override IFileHandle CreateTempFileCore(IHostEnvironment env, string s
401401
return base.CreateTempFileCore(env, suffix, "TLC_" + prefix);
402402
}
403403

404-
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
404+
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, bool verbose, int? conc)
405405
{
406406
Contracts.AssertValue(rand);
407407
Contracts.AssertValueOrNull(parentFullName);
@@ -472,7 +472,7 @@ public void Dispose()
472472

473473
private sealed class Host : HostBase
474474
{
475-
public Host(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
475+
public Host(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, bool verbose, int? conc)
476476
: base(source, shortName, parentFullName, rand, verbose, conc)
477477
{
478478
IsCancelled = source.IsCancelled;
@@ -494,7 +494,7 @@ protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase pare
494494
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>());
495495
}
496496

497-
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
497+
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, bool verbose, int? conc)
498498
{
499499
return new Host(source, shortName, parentFullName, rand, verbose, conc);
500500
}

src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ public abstract class HostBase : HostEnvironmentBase<TEnv>, IHost
107107
{
108108
public override int Depth { get; }
109109

110-
public IRandom Rand => _rand;
110+
public Random Rand => _rand;
111111

112112
// We don't have dispose mechanism for hosts, so to let GC collect children hosts we make them WeakReference.
113113
private readonly List<WeakReference<IHost>> _children;
114114

115-
public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
115+
public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string parentFullName, Random rand, bool verbose, int? conc)
116116
: base(source, rand, verbose, conc, shortName, parentFullName)
117117
{
118118
Depth = source.Depth + 1;
@@ -139,7 +139,7 @@ public void StopExecution()
139139
IHost host;
140140
lock (_cancelLock)
141141
{
142-
IRandom rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
142+
Random rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
143143
host = RegisterCore(this, name, Master?.FullName, rand, verbose ?? Verbose, conc ?? _conc);
144144
if (!IsCancelled)
145145
_children.Add(new WeakReference<IHost>(host));
@@ -342,7 +342,7 @@ public void RemoveListener(Action<IMessageSource, TMessage> listenerFunc)
342342
private readonly object _cancelLock;
343343

344344
// The random number generator for this host.
345-
private readonly IRandom _rand;
345+
private readonly Random _rand;
346346
// A dictionary mapping the type of message to the Dispatcher that gets the strongly typed dispatch delegate.
347347
protected readonly ConcurrentDictionary<Type, Dispatcher> ListenerDict;
348348

@@ -367,7 +367,7 @@ public void RemoveListener(Action<IMessageSource, TMessage> listenerFunc)
367367
/// <summary>
368368
/// The main constructor.
369369
/// </summary>
370-
protected HostEnvironmentBase(IRandom rand, bool verbose, int conc,
370+
protected HostEnvironmentBase(Random rand, bool verbose, int conc,
371371
string shortName = null, string parentFullName = null)
372372
: base(shortName, parentFullName, verbose)
373373
{
@@ -386,7 +386,7 @@ protected HostEnvironmentBase(IRandom rand, bool verbose, int conc,
386386
/// <summary>
387387
/// This constructor is for forking.
388388
/// </summary>
389-
protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, IRandom rand, bool verbose,
389+
protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, Random rand, bool verbose,
390390
int? conc, string shortName = null, string parentFullName = null)
391391
: base(shortName, parentFullName, verbose)
392392
{
@@ -433,12 +433,12 @@ public virtual void Dispose()
433433
public IHost Register(string name, int? seed = null, bool? verbose = null, int? conc = null)
434434
{
435435
Contracts.CheckNonEmpty(name, nameof(name));
436-
IRandom rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
436+
Random rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
437437
return RegisterCore(this, name, Master?.FullName, rand, verbose ?? Verbose, conc);
438438
}
439439

440440
protected abstract IHost RegisterCore(HostEnvironmentBase<TEnv> source, string shortName,
441-
string parentFullName, IRandom rand, bool verbose, int? conc);
441+
string parentFullName, Random rand, bool verbose, int? conc);
442442

443443
public IFileHandle OpenInputFile(string path)
444444
{

0 commit comments

Comments
 (0)