-2

I am trying to work out how to make a random number generator that outputs 4 integers going from 0 to 9 without any repeats.

enter image description here

Would like some help please i have just started coding in c# but cant find any answers to my issue

  • 4
    RNG without repeats - another way of saying that is that you want a **shuffle** (and then just consume as many items from the start of that as you need). You may have better luck looking for that term. – Damien_The_Unbeliever Jun 18 '21 at 13:38
  • 2
    please post the direct code and not pictures of it – Mong Zhu Jun 18 '21 at 13:38
  • Random rand = new Random(); int[] randNumbers = Enumerable.Range(0, 10).Select(x => new { n = x, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.n).ToArray(); – jdweng Jun 18 '21 at 13:39
  • This is being made in c#, Console, Visual Studio – Trapmaster123 Jun 18 '21 at 13:40
  • OT if you want to re-assign a value, you must not repeat the type (as that would try and create a new variable with the same name as an existing one - therefore the red squigglies). So just `val2 = rnd.Next...` – Hans Kesting Jun 18 '21 at 13:41
  • 1
    [Why not upload images of code when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – Tim Schmelter Jun 18 '21 at 13:46

2 Answers2

2

For "quick and dirty" solution you can use LINQ. For example something like this will randomly select 4 numbers in range from 0 to 9:

var random = new Random();
var numbers = Enumerable.Range(0, 10)
    .OrderBy(_ => random.Next())
    .Take(4)
    .ToList();

In case you need something more prescise and faster then you can implement Fisher–Yates shuffle for example.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    This has a bias for items earlier in the range being more likely. – Servy Jun 18 '21 at 13:43
  • @Servy can you please explain? – Guru Stron Jun 18 '21 at 13:49
  • OrderBy is a stable sort, so items earlier in the collection will be earlier in the results in the event of a collision. – Servy Jun 18 '21 at 13:50
  • Obviously this solution is also much less efficient than an actual shuffle algorithm, in addition to not actually being evenly distributed. – Servy Jun 18 '21 at 13:51
  • And of course posting answers to frequently asked questions is inappropriate. You should be voting to close as a duplicate. – Servy Jun 18 '21 at 13:52
  • 1
    @Servy thank you for the information. Also I would say bias (despite definetely being present) should be quite small. – Guru Stron Jun 18 '21 at 13:56
  • And yet still there. And given that writing the code that is *actually* random, and performs better, is super simple and widely available, there's just no reason not to use that. – Servy Jun 18 '21 at 13:58
2

You need to remove the int from the checking if it has already repeated. so:

if (val1 == val2)
{
    val2 = rnd.Next(1,11);
}

and not:

if (val1 == val2)
{
    int val2 = rnd.Next(1,11);
}

The latter will declare a new variable "val2" which exists only inside the scope of the "if" block, instead of updating the existing "val2" variable as you intended. This is called "shadowing".

Rich
  • 15,048
  • 2
  • 66
  • 119