-1

I made a small random number generator program in c# that uses the discarding strategy. The problem is it returns only a sequence of the same numbers, but they have to be random. What could be the problem?

public int[] GenerateRandomwithdiscarding(int n, int min, int max)
    {
        int j = 0;
        int[] sequence = new int[n];
        for (j=0;j<n;j++)
        {
            int count = 0;
            while(count<j)
            {
                int r = GenerateNextRandomNumber();
                r = min + r % (max + 1 - min);
                sequence[j] = r;
                count++;
            }
        }
        return sequence;
    }

And the function GenerateNextRandomNumber is already created and has no problems

My GenerateRandomNumber() looks like this (?)

public int GenerateNextRandomNumber() 
{
    int j = 24;
    int k = 55;
    int m = (int)Math.Pow(2, 32);
    long seed = DateTime.Now.Millisecond;

    long randomNumber = (j * seed + k) % m; 
    seed = randomNumber; 
    int finalRandom = (int)randomNumber; 
    return Math.Abs(finalRandom); 
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
finsters
  • 177
  • 1
  • 2
  • 10
  • 3
    I copy pasted your code and wrote my own GenerateNextRandomNumber method. That worked fine, so your other method (the one you didn't show us) is where the bug is, even though you assert it's not. Also, that "while" loop doesn't appear to be doing anything useful. –  May 26 '18 at 23:25
  • Could you provide the function definition of `GenerateRandomNumber()`, also a set of sample inputs and outputs and expected outputs. – Mor A. May 26 '18 at 23:27
  • My GenerateRandomNumber() looks like this : – finsters May 26 '18 at 23:46
  • private int j = 24; private int k = 55; private int m = (int)Math.Pow(2, 32); private long seed = DateTime.Now.Millisecond; – finsters May 26 '18 at 23:48
  • How did your GenerateNextRandomNumber look @ChadNedzlek? – finsters May 26 '18 at 23:49
  • This is my GenerateNextRandomNumber(). public int GenerateNextRandomNumber() { long randomNumber = (j * seed + k) % m; seed = randomNumber; int finalRandom = (int)randomNumber; return Math.Abs(finalRandom); } – finsters May 26 '18 at 23:58
  • Check out Microsofts `Random` class https://referencesource.microsoft.com/#mscorlib/system/random.cs note : for cryptography this is not really the best thing to use and there are more appropriate libraries – TheGeneral May 27 '18 at 00:13
  • Well it's not very random, is it: within the same millisecond it generates the same value over and over again. Many simple random generators use their previous value to calculate their next value to prevent this. – Ian Mercer May 27 '18 at 00:39
  • 1
    See also https://stackoverflow.com/questions/2143140/c-sharp-datetime-now-precision - there's no guarantee DateTime.Now changes even every millisecond so even if you did call it only once every millisecond it would still not be random. – Ian Mercer May 27 '18 at 00:41

2 Answers2

1

The frameworks Random class should be adequate for most application. I believe it uses the systems clock as seed. I used a list instead of an array so the size of the sequence could grow or shrink as necessary.

using System.Collections.Generic;
using System.Diagnostics;

 Random rnd = new Random();
        private void GetRandoms(int lowNumber, int highNumber, int numberOfItems)
        {
            List<int> sequence = new List<int>();
            for (int i = 0; i < numberOfItems; i++)
            {
                sequence.Add(rnd.Next(lowNumber, highNumber + 1));
            }
            foreach (int i in sequence)
            {
                Debug.Print(i.ToString());
            }
        }

Call this method like so...

GetRandoms(4, 55, 10);
Mary
  • 14,926
  • 3
  • 18
  • 27
-2

if you are using the Random() class in the method GenerateNextRandomNumber(). a seed is a must to generate new randoms. you may add a static variable for example

public static int seed = 0;

and the random constructor as

Random rand = new Random(seed++);
Ali Kleit
  • 3,069
  • 2
  • 23
  • 39