Lotto Zahlen (Anweisungen und Abfolgen)
Schreiben Sie ein Programm, dass 6 zufällig generierte Lotto Zahlen(1 bis 49) ausgibt. Bedenken Sie dabei, jede Zahl darf nur einmal vorkommen!
0 Kommentare
21 Lösung(en)
- c
- java
- python
- python
- lisp
- csharp
- java
- csharp
- abap
- java
- python
- javascript
- csharp
- cpp
- c
- vb
- csharp
- vb
- cpp
- cpp
- cpp
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int a,b,c,d,e,f;
void main(){
srand(time(NULL));
a = rand()%49 + 1;
b = rand()%49 + 1;
if(b == a){
while(b == a){
b = rand()%49 + 1;
}
}
c = rand()%49 + 1;
if(c == a || c == b){
while(c == a || c == b){
c = rand()%49 + 1;
}
}
d = rand()%49 + 1;
if(d == a || d == c || d ==b){
while(d == a || d == c || d ==b){
d = rand()%49 + 1;
}
}
e = rand()%49 + 1;
if(e == a || e == b || e == c || e == d){
while(e == a || e == b || e == c || e == d){
e = rand()%49 + 1;
}
}
f = rand()%49 + 1;
if(f == a || f == b || f == c || f == d || f ==e){
while(f == a || f == b || f == c || f == d || f ==e){
f = rand()%49 + 1;
}
}
printf("Lotto Zahlen: %d %d %d %d %d %d\n",a,b,c,d,e,f);
}
Lösung von: Fynn Koch (keine)
import java.util.ArrayList;
public class App {
public static void main(String[] args) {
ArrayList<String> numbers = new ArrayList<String>();
String lotteryNumbers = "";
for (int i = 1; i <= 49; i++) {
numbers.add(String.valueOf(i));
}
for (int i = 0; i < 6; i++) {
int randomNumber = (int) (Math.random() * numbers.size());
lotteryNumbers += numbers.get(randomNumber) + " ";
numbers.remove(randomNumber);
}
System.out.println(lotteryNumbers);
}
}
Lösung von: Samu El (Höhere Technische Lehranstalt und Werkmeisterschule des Schulvereins der Berg- und Hüttenschule Leoben Privatschule mit Öffentlichkeitsrecht)
# Lotto Zahlen vorschagen...
import random
anzahl = 0
def zahlen(anzahl, a, e):
liste = [0]
while anzahl > 0:
z = (random.randint(a, e)) # 1, 49 od. 1, 10 Bereich
liste.append(z)
a1 = len(liste)
liste = list(set(liste)) # doppelte entfernen
a2 = len(liste)
if a1 > a2:
anzahl += 1
anzahl -= 1
liste.sort()
del liste [0]
return liste
print('Zufallszahlen 6 aus 49:')
print('Gewinnzahlen', zahlen(6, 1, 49))
Lösung von: Alex Groeg (Freies Lernen)
import random
print(sorted(random.sample(range(1,50), 6)))
Lösung von: Houssein Fofana ()
(println (take 6 (shuffle (range 1 50))))
Lösung von: André Trobisch ()
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] zahlen = new int[6];
for (int i = 0; i < 6; i++)
{
int randomZahl = rnd.Next(1, 49);
while (zahlen.Contains(randomZahl))
randomZahl = rnd.Next(1, 49);
zahlen[i] = randomZahl;
}
foreach (var zahl in zahlen)
Console.Write(zahl + " ");
Console.ReadLine();
}
}
Lösung von: Christoph Mittermaier ()
/* Kotlin */
import java.util.stream.IntStream
import kotlin.streams.toList
fun main() {
IntStream.rangeClosed(1, 49).toList().shuffled().stream().limit(6).forEach { print("%d ".format(it)) }
}
Lösung von: Name nicht veröffentlicht
using System;
using System.Linq;
namespace Lotto
{
class Program
{
static void Main(string[] args)
{
int[] Lotto = new int[7];
Random r = new Random();
for(int i = 0; i < Lotto.Count(); i++)
{
int Zahl = 0;
while (Lotto.Contains(Zahl = r.Next(1, 49))) ;
Lotto[i] = Zahl;
}
Lotto.ToList().ForEach(x => Console.WriteLine(x));
Console.ReadKey();
}
}
}
Lösung von: Tobias Golz (Wilhelm Büchner Hochschule)
*Parameter definieren Zahl 1 bis 6
DATA: z_zahl1 TYPE i,
z_zahl2 TYPE i,
z_zahl3 TYPE i,
z_zahl4 TYPE i,
z_zahl5 TYPE i,
z_zahl6 TYPE i.
* Alle Zahlen auf 0 setzen.
z_zahl1 = 0.
z_zahl2 = 0.
z_zahl3 = 0.
z_zahl4 = 0.
z_zahl5 = 0.
z_zahl6 = 0.
DATA ran_int LIKE qf00-ran_int.
DATA g_ran_seed LIKE qf00-ran_seed.
* Ziehen der Zahl 1
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 49
ran_int_min = 1
IMPORTING
ran_int = ran_int.
z_zahl1 = ran_int.
*Ziehen der Zahl 2
Do 100 times.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 49
ran_int_min = 1
IMPORTING
ran_int = ran_int.
z_zahl2 = ran_int.
check z_zahl2 >< z_zahl1. "Kontrolle ob Zahl 2 nicht gleich Zahl 1 ist.
enddo.
*Ziehen der Zahl 3
Do 100 times.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 49
ran_int_min = 1
IMPORTING
ran_int = ran_int.
z_zahl3 = ran_int.
check z_zahl3 >< z_zahl2 AND z_zahl3 >< z_zahl1.
enddo.
*Ziehen der Zahl 4
Do 100 times.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 49
ran_int_min = 1
IMPORTING
ran_int = ran_int.
z_zahl4 = ran_int.
check z_zahl4 >< z_zahl3 AND z_zahl4 >< z_zahl2 AND z_zahl4 >< z_zahl1.
enddo.
*Ziehen der Zahl 5
Do 100 times.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 49
ran_int_min = 1
IMPORTING
ran_int = ran_int.
z_zahl5 = ran_int.
check z_zahl5 >< z_zahl4 AND z_zahl5 >< z_zahl3 AND z_zahl5 >< z_zahl2 AND z_zahl5 >< z_zahl1.
enddo.
*Ziehen der Zahl 6
Do 100 times.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 49
ran_int_min = 1
IMPORTING
ran_int = ran_int.
z_zahl6 = ran_int.
check z_zahl6 >< z_zahl5 AND z_zahl6 >< z_zahl4 AND z_zahl6 >< z_zahl3 AND z_zahl6 >< z_zahl2 AND z_zahl6 >< z_zahl1.
enddo.
*Ausgaben der Zahlen
WRITE: / 'Zahl 1 = ' && z_zahl1.
SKIP.
WRITE: / 'Zahl 2 = ' && z_zahl2.
SKIP.
WRITE: / 'Zahl 3 = ' && z_zahl3.
SKIP.
WRITE: / 'Zahl 4 = ' && z_zahl4.
SKIP.
WRITE: / 'Zahl 5 = ' && z_zahl5.
SKIP.
WRITE: / 'Zahl 6 = ' && z_zahl6.
SKIP.
Lösung von: Name nicht veröffentlicht
import java.util.ArrayList;
public class LottoZahlen {
public static void main(String[] args) {
// Liste zur Speicherung bereits gewaehlter Zahlen
ArrayList<Integer> lottozahlen = new ArrayList<Integer>();
// neue zufalls Zahlen bis man 6 hat
while (lottozahlen.size() != 6) {
// generiert eine neue Zufallszahl zwischen 1 und 49
int neuZahl = (int) (Math.random() * ((48) + 1)) + 1;
// nur hinzufügen und ausgeben, wenn es noch nicht in der Liste ist
if (lottozahlen.indexOf(neuZahl) == -1) {
lottozahlen.add(neuZahl);
System.out.printf("Ihr %d Zahl ist: %2d%n", lottozahlen.size(), neuZahl);
}
}
}
}
Lösung von: Name nicht veröffentlicht
from random import randint
lotto = [] * 6
i = 0
while i < 6:
zufallszahl = randint(1, 49)
if lotto.count(zufallszahl) != 0:
continue
else:
lotto.append(zufallszahl)
i += 1
print(sorted(lotto))
Lösung von: Peter Pan (Home Office)
// kann man so machen:
function rndLotteryNums_1() {
let nums = [],
num;
do {
num = Math.floor((Math.random() * 49) + 1);
if (nums.indexOf(num) == -1) nums.push(num);
} while (nums.length != 6);
return nums.sort(function(a, b){return a-b});
}
// oder so:
function rndLotteryNums_2() {
let nums = [],
selected = [];
for (let i = 1; i <= 49; i++) nums.push(i);
for (i = 1; i <= 6; i++)
selected.push(nums.splice(Math.floor(Math.random() * nums.length), 1)[0]);
return selected.sort(function(a, b){return a-b});
}
console.log(rndLotteryNums_1());
console.log(rndLotteryNums_2()); // lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
static IEnumerable<int> Generator() => Enumerable.Range(1, 49).OrderBy(x => Guid.NewGuid()).Take(6);
Lösung von: Jens Kelm (@JKooP)
// C++ 11
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
using namespace std;
int main()
{
vector<int> v;
for (size_t i = 1; i < 50; i++)
v.push_back(i);
auto s = chrono::system_clock::now()
.time_since_epoch().count();
auto r = default_random_engine(s);
shuffle(begin(v), end(v), r);
v.resize(6);
sort(v.begin(), v.end());
for (auto t : v)
cout << ' ' << t;
}
Lösung von: Jens Kelm (@JKooP)
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#define NUMBERS 6
#define LOTTERY_TYPE 49
void main() {
unsigned int arr[NUMBERS] = { 0, 0, 0, 0, 0, 0 };
srand(time(NULL));
unsigned int i = 0;
while (i < NUMBERS) {
bool t = false;
unsigned int r = rand() % LOTTERY_TYPE + 1;
for (unsigned int k = 0; k < NUMBERS; k++) {
if (arr[k] == r) {
t = true;
break;
}
}
if (t == true) continue;
arr[i++] = r;
printf("%d ", r);
}
}
Lösung von: Jens Kelm (@JKooP)
// NET Core 3.x
Sub Main(args As String())
Console.WriteLine(String.Join("-", Enumerable.Range(1, 49).OrderBy(Function(x) Guid.NewGuid()).Take(6).OrderBy(Function(x) x)))
End Sub
Lösung von: Jens Kelm (@JKooP)
using System;
namespace LottoZahlen
{
class Program
{
static void Main()
{
Random r = new Random();
int[] lottoNums = new int[6];
Console.Write("Lotto Zahlen: ");
for (byte i = 0; i < lottoNums.Length; i++) {
int rndNum = r.Next(1, 49);
for(byte b = 0; b < lottoNums.Length; b++) {
if (rndNum == lottoNums[i])
continue;
}
Console.Write(rndNum + " ");
}
Console.ReadKey(); // endl
}
}
}
Lösung von: Name nicht veröffentlicht
// VBA
Public Function Lottery() As Variant
Randomize
Const SIZE As Byte = 5
Dim arrNum(SIZE) As Byte
Dim pos As Byte, rand As Integer, i As Byte, exist As Boolean
pos = 0
Do While pos < SIZE + 1
rand = Rnd() * 1000 Mod 49 + 1
exist = False
For i = 0 To SIZE
If arrNum(i) = rand Then
exist = True
Exit For
End If
Next i
If Not exist Then
arrNum(pos) = rand
pos = pos + 1
End If
Loop
Lottery = arrNum
End Function
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <vector>
int main() {
srand((int)time(nullptr));
std::vector<int>nums{};
while (nums.size() != 6) {
const auto tmp{ rand() % 49 + 1 };
if (std::find(nums.begin(), nums.end(), tmp) == nums.end()) nums.push_back(tmp);
}
for(const auto& n : nums) std::cout << n << " ";
}
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <vector>
#include <algorithm>
const auto get_lottery_numbers(bool sorted = false) {
std::vector<int>nums{};
while (nums.size() != 6) {
const auto tmp{ rand() % 49 + 1 };
if (std::find(nums.begin(), nums.end(), tmp) == nums.end()) nums.push_back(tmp);
}
if (sorted) std::sort(nums.begin(), nums.end());
return nums;
}
int main() {
srand((int)time(nullptr));
for(const auto& n : get_lottery_numbers(true)) std::cout << n << " ";
}
Lösung von: Jens Kelm (@JKooP)
// C++ 23 | VS-2022
#include <iostream>
#include <ranges>
#include <random>
#include <numeric>
#include <print>
namespace rng = std::ranges;
int main() {
std::vector<size_t> nums(49);
rng::iota(nums, 1);
rng::shuffle(nums, std::random_device());
rng::for_each(nums | std::views::take(6),
[](const auto& i) { std::print("{} ", i); });
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung: