ShuffleBag (Fisher-Yates + PcgRandom)
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Anything that could be done better here?
public class ShuffleBag<T> : IEnumerable<T>
private readonly int m_endIndex;
private readonly FastRandom m_rng;
private readonly T m_values;
private int m_currentIndex;
public ShuffleBag(T values, ulong state, ulong? stream = null)
m_currentIndex = 0;
m_endIndex = (values.Length - 1);
m_rng = (stream.HasValue ? new FastRandom(state, stream.Value) : new FastRandom(state));
m_values = values;
public ShuffleBag(T values) : this(values, SecureRandom.GetUInt64())
IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
public IEnumerator<T> GetEnumerator()
var currentIndex = m_currentIndex;
var endIndex = m_endIndex;
var rng = m_rng;
var values = m_values;
while (currentIndex <= endIndex)
var randomIndex = rng.NextInt32(currentIndex, endIndex);
var temp = values[randomIndex];
values[randomIndex] = values[currentIndex];
values[currentIndex] = temp;
m_currentIndex = currentIndex++;
yield return temp;
For those that might inquire, here's the source for FastRandom and SecureRandom.
c# algorithm random shuffle
add a comment |Â
up vote
0
down vote
favorite
Anything that could be done better here?
public class ShuffleBag<T> : IEnumerable<T>
private readonly int m_endIndex;
private readonly FastRandom m_rng;
private readonly T m_values;
private int m_currentIndex;
public ShuffleBag(T values, ulong state, ulong? stream = null)
m_currentIndex = 0;
m_endIndex = (values.Length - 1);
m_rng = (stream.HasValue ? new FastRandom(state, stream.Value) : new FastRandom(state));
m_values = values;
public ShuffleBag(T values) : this(values, SecureRandom.GetUInt64())
IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
public IEnumerator<T> GetEnumerator()
var currentIndex = m_currentIndex;
var endIndex = m_endIndex;
var rng = m_rng;
var values = m_values;
while (currentIndex <= endIndex)
var randomIndex = rng.NextInt32(currentIndex, endIndex);
var temp = values[randomIndex];
values[randomIndex] = values[currentIndex];
values[currentIndex] = temp;
m_currentIndex = currentIndex++;
yield return temp;
For those that might inquire, here's the source for FastRandom and SecureRandom.
c# algorithm random shuffle
Quick observation - I see three different brace styles. Standardize on one. C# de-facto standard is opening and closing braces on their own lines. But that's not important - consistency is. So choose and go forth.
â Jesse C. Slicer
Apr 12 at 21:50
@JesseC.Slicer I hesitate to get into the braces thing but will say that I wholeheartedly agree about consistency. The style above is enforced by the IDE automatically; have personally found that defining "classes" of braces in the reformatter helps me parse code easier/quicker (can easily be undone before checking into a repo).
â Kittoes0124
Apr 12 at 22:44
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Anything that could be done better here?
public class ShuffleBag<T> : IEnumerable<T>
private readonly int m_endIndex;
private readonly FastRandom m_rng;
private readonly T m_values;
private int m_currentIndex;
public ShuffleBag(T values, ulong state, ulong? stream = null)
m_currentIndex = 0;
m_endIndex = (values.Length - 1);
m_rng = (stream.HasValue ? new FastRandom(state, stream.Value) : new FastRandom(state));
m_values = values;
public ShuffleBag(T values) : this(values, SecureRandom.GetUInt64())
IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
public IEnumerator<T> GetEnumerator()
var currentIndex = m_currentIndex;
var endIndex = m_endIndex;
var rng = m_rng;
var values = m_values;
while (currentIndex <= endIndex)
var randomIndex = rng.NextInt32(currentIndex, endIndex);
var temp = values[randomIndex];
values[randomIndex] = values[currentIndex];
values[currentIndex] = temp;
m_currentIndex = currentIndex++;
yield return temp;
For those that might inquire, here's the source for FastRandom and SecureRandom.
c# algorithm random shuffle
Anything that could be done better here?
public class ShuffleBag<T> : IEnumerable<T>
private readonly int m_endIndex;
private readonly FastRandom m_rng;
private readonly T m_values;
private int m_currentIndex;
public ShuffleBag(T values, ulong state, ulong? stream = null)
m_currentIndex = 0;
m_endIndex = (values.Length - 1);
m_rng = (stream.HasValue ? new FastRandom(state, stream.Value) : new FastRandom(state));
m_values = values;
public ShuffleBag(T values) : this(values, SecureRandom.GetUInt64())
IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
public IEnumerator<T> GetEnumerator()
var currentIndex = m_currentIndex;
var endIndex = m_endIndex;
var rng = m_rng;
var values = m_values;
while (currentIndex <= endIndex)
var randomIndex = rng.NextInt32(currentIndex, endIndex);
var temp = values[randomIndex];
values[randomIndex] = values[currentIndex];
values[currentIndex] = temp;
m_currentIndex = currentIndex++;
yield return temp;
For those that might inquire, here's the source for FastRandom and SecureRandom.
c# algorithm random shuffle
asked Apr 12 at 16:40
Kittoes0124
6511516
6511516
Quick observation - I see three different brace styles. Standardize on one. C# de-facto standard is opening and closing braces on their own lines. But that's not important - consistency is. So choose and go forth.
â Jesse C. Slicer
Apr 12 at 21:50
@JesseC.Slicer I hesitate to get into the braces thing but will say that I wholeheartedly agree about consistency. The style above is enforced by the IDE automatically; have personally found that defining "classes" of braces in the reformatter helps me parse code easier/quicker (can easily be undone before checking into a repo).
â Kittoes0124
Apr 12 at 22:44
add a comment |Â
Quick observation - I see three different brace styles. Standardize on one. C# de-facto standard is opening and closing braces on their own lines. But that's not important - consistency is. So choose and go forth.
â Jesse C. Slicer
Apr 12 at 21:50
@JesseC.Slicer I hesitate to get into the braces thing but will say that I wholeheartedly agree about consistency. The style above is enforced by the IDE automatically; have personally found that defining "classes" of braces in the reformatter helps me parse code easier/quicker (can easily be undone before checking into a repo).
â Kittoes0124
Apr 12 at 22:44
Quick observation - I see three different brace styles. Standardize on one. C# de-facto standard is opening and closing braces on their own lines. But that's not important - consistency is. So choose and go forth.
â Jesse C. Slicer
Apr 12 at 21:50
Quick observation - I see three different brace styles. Standardize on one. C# de-facto standard is opening and closing braces on their own lines. But that's not important - consistency is. So choose and go forth.
â Jesse C. Slicer
Apr 12 at 21:50
@JesseC.Slicer I hesitate to get into the braces thing but will say that I wholeheartedly agree about consistency. The style above is enforced by the IDE automatically; have personally found that defining "classes" of braces in the reformatter helps me parse code easier/quicker (can easily be undone before checking into a repo).
â Kittoes0124
Apr 12 at 22:44
@JesseC.Slicer I hesitate to get into the braces thing but will say that I wholeheartedly agree about consistency. The style above is enforced by the IDE automatically; have personally found that defining "classes" of braces in the reformatter helps me parse code easier/quicker (can easily be undone before checking into a repo).
â Kittoes0124
Apr 12 at 22:44
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Â
draft saved
draft discarded
Â
draft saved
draft discarded
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191893%2fshufflebag-fisher-yates-pcgrandom%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Quick observation - I see three different brace styles. Standardize on one. C# de-facto standard is opening and closing braces on their own lines. But that's not important - consistency is. So choose and go forth.
â Jesse C. Slicer
Apr 12 at 21:50
@JesseC.Slicer I hesitate to get into the braces thing but will say that I wholeheartedly agree about consistency. The style above is enforced by the IDE automatically; have personally found that defining "classes" of braces in the reformatter helps me parse code easier/quicker (can easily be undone before checking into a repo).
â Kittoes0124
Apr 12 at 22:44