#c_sharp #случайные_числа
Возникла проблема с функцией "Random". Иногда не выводит результат. Суть: Я сделал "рандомное" число от 1 до 100, и далее прописал с помощью "if" что будет происходить, если выпадут определенные числа. И при тестировании, иногда не выводился результат, я решил проверить, написал: else { Console.WriteLine("Error") }. В итоге с достаточно неплохим шансом выдает Error... В чём проблема? Вот код: Random rnd1 = new Random(); if (rnd1.Next(0, 100) == 1 || rnd1.Next(1, 100) == 2 || rnd1.Next(1, 100) == 3 || rnd1.Next(1, 100) == 4 || rnd1.Next(1, 100) == 5 || rnd1.Next(1, 100) == 6 || rnd1.Next(1, 100) == 7 || rnd1.Next(1, 100) == 8 || rnd1.Next(1, 100) == 9 || rnd1.Next(1, 100) == 10 || rnd1.Next(1, 100) == 11 || rnd1.Next(1, 100) == 12 || rnd1.Next(1, 100) == 13 || rnd1.Next(1, 100) == 14 || rnd1.Next(1, 100) == 15 || rnd1.Next(1, 100) == 16 || rnd1.Next(1, 100) == 17 || rnd1.Next(1, 100) == 18 || rnd1.Next(1, 100) == 19 || rnd1.Next(1, 100) == 20 || rnd1.Next(1, 100) == 21 || rnd1.Next(1, 100) == 22 || rnd1.Next(1, 100) == 23 || rnd1.Next(1, 100) == 24 || rnd1.Next(1, 100) == 25 || rnd1.Next(1, 100) == 26 || rnd1.Next(1, 100) == 27 || rnd1.Next(1, 100) == 28 || rnd1.Next(1, 100) == 29 || rnd1.Next(1, 100) == 30 || rnd1.Next(1, 100) == 31 || rnd1.Next(1, 100) == 32 || rnd1.Next(1, 100) == 33 || rnd1.Next(1, 100) == 34 || rnd1.Next(1, 100) == 35) { Console.WriteLine("1"); } else if (rnd1.Next(0, 100) == 36 || rnd1.Next(1, 100) == 37 || rnd1.Next(1, 100) == 38 || rnd1.Next(1, 100) == 39 || rnd1.Next(1, 100) == 40 || rnd1.Next(1, 100) == 41 || rnd1.Next(1, 100) == 42 || rnd1.Next(1, 100) == 43 || rnd1.Next(1, 100) == 44 || rnd1.Next(1, 100) == 45 || rnd1.Next(1, 100) == 46 || rnd1.Next(1, 100) == 47 || rnd1.Next(1, 100) == 48 || rnd1.Next(1, 100) == 49 || rnd1.Next(1, 100) == 50 || rnd1.Next(1, 100) == 51 || rnd1.Next(1, 100) == 52 || rnd1.Next(1, 100) == 53 || rnd1.Next(1, 100) == 54 || rnd1.Next(1, 100) == 55 || rnd1.Next(1, 100) == 56 || rnd1.Next(1, 100) == 57 || rnd1.Next(1, 100) == 58 || rnd1.Next(1, 100) == 59 || rnd1.Next(1, 100) == 60 || rnd1.Next(1, 100) == 61 || rnd1.Next(1, 100) == 62 || rnd1.Next(1, 100) == 63 || rnd1.Next(1, 100) == 64 || rnd1.Next(1, 100) == 65) { Console.WriteLine("2"); } else if (rnd1.Next(0, 100) == 66 || rnd1.Next(1, 100) == 67 || rnd1.Next(1, 100) == 68 || rnd1.Next(1, 100) == 69 || rnd1.Next(1, 100) == 70 || rnd1.Next(1, 100) == 71 || rnd1.Next(1, 100) == 72 || rnd1.Next(1, 100) == 73 || rnd1.Next(1, 100) == 74 || rnd1.Next(1, 100) == 75 || rnd1.Next(1, 100) == 76 || rnd1.Next(1, 100) == 77 || rnd1.Next(1, 100) == 78 || rnd1.Next(1, 100) == 79 || rnd1.Next(1, 100) == 80 || rnd1.Next(1, 100) == 81 || rnd1.Next(1, 100) == 82 || rnd1.Next(1, 100) == 83 || rnd1.Next(1, 100) == 84 || rnd1.Next(1, 100) == 85) { Console.WriteLine("3"); } else if (rnd1.Next(0, 100) == 86 || rnd1.Next(0, 100) == 87 || rnd1.Next(0, 100) == 88 || rnd1.Next(0, 100) == 89 || rnd1.Next(0, 100) == 90 || rnd1.Next(0, 100) == 91 || rnd1.Next(0, 100) == 92 || rnd1.Next(0, 100) == 93 || rnd1.Next(0, 100) == 94 || rnd1.Next(0, 100) == 95 || rnd1.Next(0, 100) == 96 || rnd1.Next(0, 100) == 97 || rnd1.Next(0, 100) == 98 || rnd1.Next(0, 100) == 99 || rnd1.Next(0, 100) == 100) { Console.WriteLine("4"); } else { Console.WriteLine("Error"); } string theEnd = Console.ReadLine();
Ответы
Ответ 1
(Не будем хаять невинный Random.) Вы все время сравниваете с константами разные числа. Random rnd1 = new Random(); int value = rnd1.Next(0, 100); if (value >= 1 && value <= 35) { Console.WriteLine("1"); } else if (... И почему у Вас в некоторых вызовах Next нижняя граница 0, а в некоторых 1? Надо int value = rnd1.Next(0, 100) + 1; или int value = rnd1.Next(1, 101);
Комментариев нет:
Отправить комментарий