Würfel (1) (Simulationen)
Suchen Sie in der APIAPI = Application Programmer Interface (Programmierschnittstelle an das System)-Dokumentation Ihrer Programmiersprache (z. B. Java-API) einen Weg, Pseudozufallszahlen zu generieren. Schreiben Sie ein Programm, das 100 Würfelresultate (Zahlen von 1 bis 6) simuliert. Prüfen Sie Ihr Resultat, indem Sie zählen, wie oft jede Zahl dabei vorgekommen ist.
0 Kommentare
5 Lösung(en)
/**
* @author Philipp Gressly (phi AT gressly DOT ch)
*/
public class Wuerfel1 {
public static void main(String[] args) {
new Wuerfel1().top();
}
void top() {
int wurfNr = 1;
while(wurfNr <= 100) {
double nullEins = Math.random();
int wurf = (int) (nullEins * 6 + 1);
System.out.println("Wurf: " + wurf);
wurfNr = wurfNr + 1;
}
}
} // end of class Wuerfel1
import random, collections
wuerfe = []
n = 1
while n <= 100:
wurf = random.randint(1,6)
wuerfe.append(wurf)
n += 1
c = collections.Counter()
for i in wuerfe:
c[i] += 1
print(c)
Lösung von: Alex Groeg (Freies Lernen)
let results = ['X', 0, 0, 0, 0, 0, 0];
for (let i = 1; i <= 100; i++)
results[Math.floor((Math.random() * 6) + 1)]++;
console.table(results); // lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
Enumerable.Range(1, 100)
.Select(x => new Random().Next(1, 7))
.GroupBy(x => x).Select(x => (n: x.Key, c: x.Count()))
.OrderBy(x => x.n).ToList().ForEach(x => Console.WriteLine(x));
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
constexpr size_t MAX_NUM{ 6 };
int main() {
srand((int)time(nullptr));
size_t arr[MAX_NUM]{};
for (size_t i{ 0 }; i < 100; i++)
arr[rand() % MAX_NUM ]++;
for (size_t i{ 0 }; i < MAX_NUM; i++)
std::cout << i + 1 << ": " << arr[i] << "\n";
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 1 |
Schwierigkeit: | k.A. |
Webcode: | r2hn-k3om |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |