L
L
lue merg2018-02-09 17:02:13
C++ / C#
lue merg, 2018-02-09 17:02:13

How to fill an array with unique values?

There is an array and it must be filled with numbers in the range, for example, from 1 to 10, in random order.
For example, element a[1] contains 7, element a[2] contains 4.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Daniil Basmanov, 2018-02-09
@luemerg

You open the documentation for System.Random and read the examples, it's just about numbers.

1
15432, 2018-02-09
@15432

We create a list of numbers from 1 to 10, then we cut out elements from it at random indices and write them to our array.

L
LiptonOlolo, 2018-02-09
@LiptonOlolo

one.

Random rn = new Random();
var array = Enumerable.Range(0, 10).Select(x => rn.Next(255)).ToArray();

2.
var array = Enumerable.Range(0, 10).OrderBy(x => Guid.NewGuid()).ToArray();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question