1

Referring to generating random numbers without consecutive repetition, I want to make a non-repeating random integer generator but between 2 to 9 inclusive.

I make good use of the wrapping code from the answer in the above url.

int oldrand = <prior random number>;
int adder = randomNumberGenerator() % 4;
int newrand = (oldrand + adder + 1) % 5;

My code, however,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Random Rand = new Random();
            int oldrand = Rand.Next(0,7);
            //Console.WriteLine(oldrand);
            for (int i=0; i<50;i++) {

            int adder = Rand.Next(0,7) % 7;
            int newrand = (oldrand + adder + 1) % 8+2;
            int newrand2 = (newrand + adder + 1) % 8+2;
            int newrand3 = (newrand2 + adder + 1) % 8+2;
            int newrand4 = (newrand3 + adder + 1) % 8+2;

            Console.WriteLine(newrand);
            Console.WriteLine(newrand2);
            Console.WriteLine(newrand3);
            Console.WriteLine(newrand4);
            oldrand = newrand4;
            Console.WriteLine();
            }
        }
    }

    public class Rand {
        private static readonly Random globalRandom = new Random(); 
        private static readonly object globalLock = new object();

        private static readonly ThreadLocal<Random> threadRandom = new ThreadLocal<Random>(NewRandom);

        public static Random NewRandom() 
        { 
            lock (globalLock) 
            { 
                return new Random(globalRandom.Next()); 
            } 
        }

        public static Random Instance { get { return threadRandom.Value; } }

        public static int Next(int minValue, int maxValue) 
        { 
            return Instance.Next(minValue, maxValue); 
        }

    }
}

generates a strange result:

2
7
4
9

2
3
4
5

2
7
4
9

5
9
5
9

8
7
6
5

3
9
7
5

2
7
4
9

5
9
5
9

5
9
5
9

9
9
9
9

5
9
5
9

7
5
3
9

8
7
6
5

3
9
7
5

9
5
9
5

4
3
2
9

6
3
8
5

2
7
4
9

6
3
8
5

9
5
9
5

As you can see, 5 and 9 repeat like a pattern while in another sequence you see all four 9's.

Community
  • 1
  • 1

1 Answers1

0

The problem with your code is that you need to pick a new random number for each new random number you want. Your code reuses the single random number over and over, so you just get a mathematically predictable sequence, and depending on where you start, you'll get duplicates.

Here's a version of the code that works (and tests the results…it's also written in a way I think is much more readable, reusable, and understandable):

    static void Main(string[] args)
    {
        Random random = new Random();
        const int max = 8;

        for (int i = 0; i < 50; i++)
        {
            int[] sequence =
                GetRandomNonConsecutiveSequence(random, max, 4)
                .Select(j => j + 2)
                .ToArray();

            if (!CheckSequence(sequence))
            {
                Console.WriteLine("Sequence failed: " + string.Join(", ", sequence));
            }
        }
    }

    private static bool CheckSequence(int[] sequence)
    {
        int previous = sequence[0];

        for (int i = 1; i < sequence.Length; i++)
        {
            if (previous == sequence[i])
            {
                return false;
            }

            previous = sequence[i];
        }

        return true;
    }

    private static IEnumerable<int> GetRandomNonConsecutiveSequence(
        Random random, int max, int count)
    {
        int previous = random.Next(max);

        yield return previous;

        while (--count > 0)
        {
            yield return previous = Next(previous, max, random);
        }
    }

    static int Next(int previous, int max, Random random)
    {
        return (previous + random.Next(max - 1) + 1) % max;
    }
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136