Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:28:07+00:00 2026-05-25T15:28:07+00:00

I am working on a static class to provide random values for my program,

  • 0

I am working on a static class to provide random values for my program, but there are some issues with implementing the equivalent of the Random.Next(int maxValue) functionality:

public class CRandom {
    static readonly byte[] randomSet;
    static readonly Func<int> closedIndex; 
    static CRandom() {
        randomSet = new byte[60000000];
        closedIndex = _ClosedIndex(0); 
        RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
        Gen.GetBytes(randomSet);
    }
    public static int Next() {
        int index = closedIndex();
        return Convert.ToInt32(randomSet[index]);
    }
    public static int Next(int maxValue) {
        int index = closedIndex();
        byte[] remainingSet = randomSet.Skip(index + 1).ToArray();
        byte next = remainingSet.First(x => Convert.ToInt32(x) < maxValue); 
        return Convert.ToInt32(next);
    }
    public static Func<int> _ClosedIndex(int seed) {
        // seed is the initial value
        int _index = seed - 1; 
        Func<int> del = new Func<int>(() =>
        {  // always returns auto-incremented value
            _index++; 
            return _index; 
        });
        return del; 
    }
}

Basically what it does is fill up a static/readonly byte array of random values and in the case of the Next(maxValue) method just gets the next value thats in range but hasn’t been used before. However trying out Next(100) in a loop is giving these results, which obviously aren’t random:

53
20
20
34
34
73
73
73
73

This is also a very slow way of doing it. I’m sure there is a better way but I don’t know quite how Random.Next() works under the hood.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:28:08+00:00Added an answer on May 25, 2026 at 3:28 pm

    MSDN Magazine has a very detailed article about this exact topic.
    It’s more complicated than you think.

    He wrote the following class; however, read the article for important notes about randomness.

    public class CryptoRandom : Random
    {
        private RNGCryptoServiceProvider _rng =
            new RNGCryptoServiceProvider();
        private byte[] _uint32Buffer = new byte[4];
    
        public CryptoRandom() { }
        public CryptoRandom(Int32 ignoredSeed) { }
    
        public override Int32 Next()
        {
            _rng.GetBytes(_uint32Buffer);
            return BitConverter.ToInt32(_uint32Buffer, 0) & 0x7FFFFFFF;
        }
    
        public override Int32 Next(Int32 maxValue)
        {
            if (maxValue < 0)
                throw new ArgumentOutOfRangeException("maxValue");
            return Next(0, maxValue);
        }
    
        public override Int32 Next(Int32 minValue, Int32 maxValue)
        {
            if (minValue > maxValue) 
                throw new ArgumentOutOfRangeException("minValue");
            if (minValue == maxValue) return minValue;
            Int64 diff = maxValue - minValue;
            while (true)
            {
                _rng.GetBytes(_uint32Buffer);
                UInt32 rand = BitConverter.ToUInt32(_uint32Buffer, 0);
    
                Int64 max = (1 + (Int64)UInt32.MaxValue);
                Int64 remainder = max % diff;
                if (rand < max - remainder)
                {
                    return (Int32)(minValue + (rand % diff));
                }
            }
        }
    
        public override double NextDouble()
        {
            _rng.GetBytes(_uint32Buffer);
            UInt32 rand = BitConverter.ToUInt32(_uint32Buffer, 0);
            return rand / (1.0 + UInt32.MaxValue);
        }
    
        public override void NextBytes(byte[] buffer)
        {
            if (buffer == null) throw new ArgumentNullException("buffer");
            _rng.GetBytes(buffer);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question, how make this code working ? public class T { public static
I currently have this working but it requires me to have a static method
Here's the domain classes I'm working with: class Foo { String name, type static
I'm having some problems with ActiveRecord. Well everything works fine, but it's working sometimes.
I am working on project in Linux which involves 1) Static Lib in C++
Working in C# and Java, I've seen basically one way everybody initializes singletons: static
specifically, will working with containers as opposed to the static ObjectFactory let me keep
I'm working on a Java project where I have created a class that looks
I am defining a static factory method with: @XmlType(factoryClass=DummyFactory.class, factoryMethod=createNew) public abstract MyClass() {
I'm back with another similar question. I am currently working on a Java program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.