Загрузка...

Array of 10 random numbers: all numbers must be different

Thread in C# created by mikef0x Mar 16, 2019. 146 views

  1. mikef0x
    mikef0x Topic starter Mar 16, 2019 Кодер C# 0 Jan 31, 2019
    Есть код 10 рандомных чисел с диапазоном 15.
    Нужно сделать так что бы при генераций этих чисел, числи не совпадали.
    Все числа должны быть разные, нужно сделать через цикл For
    Помогите а)
    Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp2 {
    class Program {
    static void Main(string[] args) {
    Random r = new Random();
    int[] array = new int[10];
    for (int i = 0; i < array.Length; i++) {
    array[i] = r.Next(15);
    }
    for (int i = 0; i < array.Length; i++) {
    Console.WriteLine(array[i]);
    }
    Console.ReadKey();
    }

    }

    }
    Только через for, if, elseif, else
    Помогите а(
     
  2. Raidor
    Raidor Mar 16, 2019 Road 35 Jul 24, 2018
    Не ?
    Code


    using System;
    using System.Linq;
    using System.Collections.Generic;

    class Program
    {
    private static Random RND = new Random();

    public static void Main()
    {
    List<int> arr = new List<int>(Enumerable.Range(10, 91));
    int[,] mat = new int[10, 8];
    for (int i = 0; i < mat.GetLength(0); ++i) {
    for (int j = 0; j < mat.GetLength(1); ++j) {
    int k = RND.Next(arr.Count);
    mat[i, j] = arr[k];
    arr[k] = arr[arr.Count - 1];
    arr.RemoveAt(arr.Count - 1);
    }
    }
    for (int i = 0; i < mat.GetLength(0); ++i) {
    for (int j = 0; j < mat.GetLength(1); ++j) {
    Console.Write($"{mat[i, j]}{j == mat.GetUpperBound(1) ? '\n' : '\t'}");
    }
    }
    }
    }
     
Top
Loading...