Skip to content

Instantly share code, notes, and snippets.

View gpc91's full-sized avatar

George gpc91

  • Steel City Interactive
  • Sheffield, United Kingdom
View GitHub Profile
@gpc91
gpc91 / RandomExtensions.cs
Created November 30, 2022 15:49
System.Random Next ulong (UInt64) extension
/// <summary>
/// Extension methods for the <see cref="System.Random"/> class.
/// </summary>
public static class RandomExtensions
{
/// <summary>
/// Returns a random unsigned ulong
/// </summary>
public static UInt64 NextUInt64(this Random random)
{
@gpc91
gpc91 / Eratosthene.cs
Created June 24, 2022 19:07
C# Prime Sieve
/*
* Sieve of Eratosthene, C# implementation by George Corkery (github.com/gpc91)
* Something made for fun, feel free to use without attribution
* For more about the Sieve of Eratosthene, see this wikipedia page: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
*/
using System;
using System.Collections.Generic;
class Eratosthene
{
@gpc91
gpc91 / NumberToBinary.cs
Last active January 21, 2022 08:45
Convert a Numerical Type to a binary string and pad it properly.
string NumberToBinary<T>(T val) where T : notnull, IConvertible
{
try
{
long value = val.ToInt64(null);
int size = System.Runtime.InteropServices.Marshal.SizeOf(default(T));
return BitConverter.IsLittleEndian ?
Convert.ToString(value, 2).PadLeft(size * 8, '0') :
Convert.ToString(value, 2).PadRight(size * 8, '0');
}