Last active
July 8, 2021 14:11
-
-
Save subena22jf/c7bb027ea99127944981 to your computer and use it in GitHub Desktop.
c# Random with long values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace System.Maths | |
{ | |
public class Random | |
{ | |
public int index; | |
public void init() | |
{ | |
System.Random rd = new System.Random(); | |
} | |
public int Next(int Min, int Max) | |
{ | |
System.Random rd = new System.Random(); | |
return rd.Next(Min, Max); | |
} | |
public long Next(long min,long max) | |
{ | |
System.Random rd = new System.Random(); | |
if (max <= min) | |
throw new ArgumentOutOfRangeException("max", "max must be > min!"); | |
ulong uRange = (ulong)(max - min); | |
ulong ulongRand; | |
do | |
{ | |
byte[] buf = new byte[8]; | |
rd.NextBytes(buf); | |
ulongRand = (ulong)BitConverter.ToInt64(buf, 0); | |
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); | |
return (long)(ulongRand % uRange) + min; | |
} | |
public long Next(long max) | |
{ | |
return Next(0, max); | |
} | |
public int Next(int Max) | |
{ | |
System.Random rd = new System.Random(); | |
return rd.Next(Max); | |
} | |
public byte NextBytes(byte[] buffer) | |
{ | |
Random rnd = new Random(); | |
return rnd.NextBytes(buffer); | |
} | |
public double NextDouble() | |
{ | |
System.Random rd = new System.Random(); | |
return rd.NextDouble(); | |
} | |
public string RandomString(int size, bool lowerCase) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
System.Random random = new System.Random(); | |
char ch; | |
for (int i = 0; i < size; i++) | |
{ | |
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); | |
builder.Append(ch); | |
} | |
if (lowerCase) | |
return builder.ToString().ToLower(); | |
return builder.ToString(); | |
} | |
public object NextArrayList(ArrayList Arrays){ | |
System.Random rd = new System.Random(); | |
int rand = rd.Next(0, Arrays.Count); | |
var value = Arrays[rand]; | |
} | |
public IEnumerable<T> Randomize<T>(this IEnumerable<T> source) | |
{ | |
System.Random rnd = new System.Random(); | |
return source.OrderBy<T, int>((item) => rnd.Next()); | |
} | |
} | |
} |
Hello,
This source code is helpful to me. Thanks for sharing.
John
Do you have Skype name? I have some questions need to clarify.
Thank you
John
hopefully help you,
Skype name is my username: subena22jf
Very useful 👍
What's the licensing on this? It's just what I need, I just want to make sure that it's OK for me to use. I'm putting in research code (not something that would ever be sold), but for a commercial company (Microsoft).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hio...add random string with filter
So here we have two methods; one for Unicode and one for ASCII. My thought was that when generating a simple password for US English keyboards, I’d just use a simple ASCII generator using characters available on the keyboard. For for the rest of you folks who don’t have a US English keyboard, maybe there are some options to generate a random string with a custom range filter.
public string GetRandomUnicodeString(int length, int maxValue, Predicate valueFilter)
{
byte[] seedBuff = new byte[4];
byte[] charBuff;
}
public string GetRandomASCIIString(int length)
{
return GetRandomUnicodeString(length, 0x7E, o => o >= 0x21 && o <= 0x7E);
//return GetRandomUnicodeString(length, 126, o => o >= 33 && o <= 126); //could use integers instead
}
when using the Unicode method, you have to specify a max value; otherwise, the random generator will choke if you want a small range of numbers when randomizing a pool of 65,535 values. So the cap will help performance. The filter expression should filter out the random results so you only get the characters you want.
The ASCII method is a pretty simple example of how this works. I want a max value of 126, and I want characters in the range of 33 through 126. I used hex (because the Windows character map uses hex).
Thanks Nathan Bridgewater for this share