Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualalvaro

Posted 11 October 2012 - 03:30 AM

This version is slightly more elegant:
import java.util.Random;

public class flip {
		static Random random_generator;
		
		static void flip_coin(int repeat) {
				if ( repeat > 0) {
					double random_number = random_generator.nextDouble();
					System.out.println(random_number);
					
					if (random_number < 0.5)
						System.out.println("head");
					else
						System.out.println("tail");

					flip_coin(repeat-1); // <-- This is the only change
				}
		}
		
		public static void main (String[] args) {
				random_generator = new Random();
				flip_coin(10);
		}
}

#2alvaro

Posted 11 October 2012 - 03:30 AM

This version is slightly more elegant:
[source lang="java"]import java.util.Random;public class flip { static Random random_generator; static void flip_coin(int repeat) { if ( repeat > 0) { double random_number = random_generator.nextDouble(); System.out.println(random_number); if (random_number < 0.5) System.out.println("head"); else System.out.println("tail"); flip_coin(repeat-1); // <-- This is the only change } } public static void main (String[] args) { random_generator = new Random(); flip_coin(10); }}[/source]

#1alvaro

Posted 11 October 2012 - 03:29 AM

This version is slightly more elegant:
[source lang="java"]import java.util.Random;public class flip { static Random random_generator; static void flip_coin(int repeat) { if ( repeat > 0) { double random_number = random_generator.nextDouble(); System.out.println(random_number); if (random_number < 0.5) System.out.println("head"); else System.out.println("tail"); flip_coin(repeat-1); // <-- %his is the only change } } public static void main (String[] args) { random_generator = new Random(); flip_coin(10); }}[/source]

PARTNERS