Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › C# Programming › Simple random class with one time occurring random numbers!

Simple random class with one time occurring random numbers!

Posts 1–5 of 5 · Page 1 of 1
Laslod
Laslod
Simple random class with one time occurring random numbers!
I made a simple random class so that each number occurs one time. This way, you can get a random number without fear of one number occurring more than once. If you wish to do so, you can make the classes static too.

Code:
class MyOwnRandom
    {
        
        List<int> alreadyCalledNumbers = new List<int>();
        
        Random random = new Random();

        public int next(int min, int max)
        {
            int randomNumber;
            randomNumber = random.Next(min,max);
            
            while(alreadyCalledNumbers.Contains(randomNumber))
            {
                randomNumber = random.Next(min, max);
            }

            alreadyCalledNumbers.Add(randomNumber);
            return randomNumber;
        }
        public int next(int max)
        {
            int randomNumber;
            randomNumber = random.Next(max);

            while (alreadyCalledNumbers.Contains(randomNumber))
            {
                randomNumber = random.Next(max);
            }

            alreadyCalledNumbers.Add(randomNumber);
            return randomNumber;
        }
        public void reset()
        {
            alreadyCalledNumbers.Clear();
        }
    }
#1 · 13y ago
Jason
Jason
Quote Originally Posted by Laslod View Post
I made a simple random class so that each number occurs one time. This way, you can get a random number without fear of one number occurring more than once. If you wish to do so, you can make the classes static too.

Code:
class MyOwnRandom
    {
        
        List<int> alreadyCalledNumbers = new List<int>();
        
        Random random = new Random();

        public int next(int min, int max)
        {
            int randomNumber;
            randomNumber = random.Next(min,max);
            
            while(alreadyCalledNumbers.Contains(randomNumber))
            {
                randomNumber = random.Next(min, max);
            }

            alreadyCalledNumbers.Add(randomNumber);
            return randomNumber;
        }
        public int next(int max)
        {
            int randomNumber;
            randomNumber = random.Next(max);

            while (alreadyCalledNumbers.Contains(randomNumber))
            {
                randomNumber = random.Next(max);
            }

            alreadyCalledNumbers.Add(randomNumber);
            return randomNumber;
        }
        public void reset()
        {
            alreadyCalledNumbers.Clear();
        }
    }
Would hit infinite looping really easily.

Code:
var rnd = new MyOwnRandom();
for(int i = 0; i < 10; ++i)
    Console.WriteLine(rnd.next(1,5));
Would never work. I'm not quite sure when you'd need something like this to be quite honest.
#2 · 13y ago
Laslod
Laslod
Quote Originally Posted by Jason View Post


Would hit infinite looping really easily.

Code:
var rnd = new MyOwnRandom();
for(int i = 0; i < 10; ++i)
    Console.WriteLine(rnd.next(1,5));
Would never work. I'm not quite sure when you'd need something like this to be quite honest.
When you want a random number but don't want it to appear more than once. Let's say you have an enum with names, and you want to get a random amount of names from that enum. You wouldn't want a name to appear twice, so you can use something like this to get a random name and make sure that name doesn't come up again.
#3 · 13y ago
Jason
Jason
Quote Originally Posted by Laslod View Post
When you want a random number but don't want it to appear more than once. Let's say you have an enum with names, and you want to get a random amount of names from that enum. You wouldn't want a name to appear twice, so you can use something like this to get a random name and make sure that name doesn't come up again.
If you were using an enum to store names...that's a rather weird way of doing. Also, you could just iterate over an array to get distinct values, shuffling the array if necessary. For example:

Code:
public class RandomizedCollection<TInput>
    {
        // Random provider
        [ThreadStatic]
        private static Random _random;
        private static Random Random { get { return _random ?? (_random = new Random(System.Threading.Interlocked.Increment(ref _seed))); } }
        private static int _seed = Environment.TickCount;

        // Allow for progressive stepping through the collection, with lazy evaluation
        private readonly IEnumerator<TInput> _enumerator;
        private bool _hasNext;

        public bool HasNext { get { return _hasNext; } }

        public RandomizedCollection(IEnumerable<TInput> original)
        {
            _enumerator = original.OrderBy(t => Random.Next()).GetEnumerator();
            _hasNext = _enumerator.MoveNext(); // move to the beginning of the collection
        }

        public TInput Next()
        {
            if (!_hasNext)
                throw new InvalidOperationException("Reached the end of the collection");

            var value = _enumerator.Current;
            _hasNext = _enumerator.MoveNext();
            return value;
        }
    };
Would be a fairly trivial way of getting each value of a collection ONCE-ONLY, in a random order.

For example:
Code:
var stuff = new[] {
                "hello",
                "world",
                "this",
                "is",
                "a",
                "random",
                "string",
                "array"
            };

            var randomized = new RandomizedCollection<string>(stuff);
            while(randomized.HasNext())
                Console.WriteLine(randomized.Next());
            Console.Read();
Of course, this is really unnecessary encapsulation, as it's essential just a wrapper around something like:
Code:
var rnd = new Random(Environment.TickCount);
foreach(var item in stuff.OrderBy(s => rnd.Next()))
    Console.WriteLine(item);
But you get the idea.
#4 · 13y ago
Laslod
Laslod
Quote Originally Posted by Jason View Post


If you were using an enum to store names...that's a rather weird way of doing. Also, you could just iterate over an array to get distinct values, shuffling the array if necessary.


But you get the idea.
Oh yea I know I can use arrays but this way I don't have to. I can reuse it the class whenever I want and can use it for lots of other things but I get your idea.
#5 · 13y ago
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

  • CA opens with hacks but then randomly closes out usualy before i can start a game...By blazeboy888 in Combat Arms Help
    7Last post 16y ago
  • Binding a key so it uses the command multiple times with one pressBy m202 in Vindictus Help
    9Last post 15y ago
  • Having trouble with one weapon class hack... Everybody enter and have fun !By kmanev073 in CrossFire Hack Coding / Programming / Source Code
    8Last post 14y ago
  • running 2 warrock with one account at the same time?By lagger in WarRock Help
    3Last post 15y ago
  • Random class generatorBy schiz in Call of Duty Modern Warfare 3 Help
    1Last post 14y ago

Tags for this Thread

None