ShuffleBag (Fisher-Yates + PcgRandom)

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question



















  • 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

















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.







share|improve this question



















  • 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













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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









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

















  • 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
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?