Skip to content

Instantly share code, notes, and snippets.

@lukepuplett
Created August 7, 2019 17:31
Show Gist options
  • Save lukepuplett/728929569be94b0d7525e14418550d85 to your computer and use it in GitHub Desktop.
Save lukepuplett/728929569be94b0d7525e14418550d85 to your computer and use it in GitHub Desktop.
How to round robin writing to an array
int length = 10;
var numbers = new int[length];
int r = 0;
for (int i = 0; i < 100000; i++)
{
r = unchecked(r + 1);
numbers[r % length] = r;
}
@lukepuplett
Copy link
Author

I think .NET integers are unchecked by default so this might work, too.

numbers[r++ % length] = value;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment