Simple random class with one time occurring random numbers!

Posts 15 of 5 · Page 1 of 1
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();
        }
    }
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.
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.
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.
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.
Posts 15 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?