Ignora collegamentiHome / Blog

Blog


How to generate six lucky numbers with Java

If you want to win SuperEnalotto can use this simple program written in Java to extract the six lucky numbers and become millionaires.

import java.util.*;

public class SuperEnalotto
{  
	public static void main(String[] args)
	{  
	int numbersToBeExtracted = 6;
	int maximumValueOfTheNumber = 90;

	int[] numbers = new int[maximumValueOfTheNumber];
	for (int j = 0; j < numbers.length; j++)
	numbers[j] = j + 1;

	int[] numbersDrawn = new int[numbersToBeExtracted];
	for (int j = 0; j < numbersDrawn.length; j++)
	{  
	int randomNumber = (int) (Math.random() * maximumValueOfTheNumber);
	numbersDrawn [j] = numbers[randomNumber];
	numbers[randomNumber] = numbers[maximumValueOfTheNumber - 1];
	maximumValueOfTheNumber--;
	}

	Arrays.sort(numbersDrawn);
	System.out.println("Your lucky numbers:");
	for (int n = 0; n<numbersDrawn.length; n++)
	System.out.print("[" + numbersDrawn[n] + "] ");
	System.out.println();
	}
}





Category: Java