Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
moneda
/* Simulación del lanzamiento de una moneda Ley de Los Grandes Números Autor: Enrique Benimeli Publicado en www.esferatic.com */ /* Copyright (C) 2012 Enrique Benimeli This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.*; import java.lang.*; import java.util.Random; class Rextester { public static void main(String args[]) { // Definimos el experimento... int t = 50000; // número de lanzamientos int b = 1; // resultado que se anota: 0 es cara, 1 es cruz boolean verLanzamientos = true; // valor false si sólo se quiere ver el resultado final String [] caracruz = {"C","X"}; System.out.println("Simulando " + t + " lanzamientos de moneda..."); System.out.print("El programa cuenta el número de '"); System.out.println(caracruz[b] + "' que salen..."); // nos preparamos para generar un número aleatorio... Random generator = new Random(); // definimos un contador de lanzamientos y casos favorables double lanzamientos = 0; double favorables = 0; // empiezan los lanzamientos de moneda... for(int i=0;i<(t/10);i++) { for(int j=0;j<10;j++) { // lanzamos... int r = generator.nextInt( 2 ); if(r == b) { // si el lanzamiento es el resultado que esperábamos, // lo mostramos y señalamos entre paréntesis favorables++; if(verLanzamientos) System.out.print("(" + caracruz[r] + ") "); } else { // si no, simplemente lo mostramos if(verLanzamientos) System.out.print(" " + caracruz[r] + " "); } } // nuevo bloque de 10 lanzamientos... lanzamientos+=10; // mostramos la frecuencia relativa hasta el momento if(verLanzamientos) { verLanzamiento(i,favorables,lanzamientos); } // la frecuencia relativa hasta el último lanzamiento // siempre se muestra if( i==((t/10)-1)) { verLanzamiento(i,favorables,lanzamientos); } } } // Se muestran los detalles del lanzamiento y la frecuencia relativa public static void verLanzamiento(int i, double favorables, double lanzamientos) { // calculamos la frecuencia relativa double fr = favorables/lanzamientos; System.out.print("\t" + (int)favorables + "/" + (int)lanzamientos); System.out.println("\t= " + fr + "\t "); } }
run
|
edit
|
history
|
help
0
"encryption"
1(D).
Java Lambda Expression Sample
inheritance
evenloop
mp1
1a
Compiler
Counter.java
Frequency