Although I should be writing code to make turtles dance (it's an assignment), I couldn't help but respond to an email from an online Java tutorial that I use. The sample code in the email was about generating unique random numbers for a lottery. It struck me that I have just enough programming skill to modify that tutorial into something useful for me - generate the winning powerball number!!!!
Here's the code - good luck!
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
/**
*Create an arraylist of lottery numbers, sort, and pick the winning numbers
*
*
* via About.com Java
*/
public class Lottery
{
public static void main(String[] args)
{
//define ArrayList to hold Integer objects
//game uses 1 - 59 for 1st 5 numbers
ArrayList<Integer> numbers = new ArrayList<Integer>();
for(int i=0; i<59; i++)
{
numbers.add(i + 1);
}
//define ArrayList to hold Integer objects
//game uses 1 - 39 for powerball number
ArrayList<Integer> powerball = new ArrayList<Integer>();
for(int i=0; i<39; i++)
{
powerball.add(i+1);
}
//shuffle numbers and powerballs
Collections.shuffle(numbers);
Collections.shuffle(powerball);
//import numbers into a list and extract 1st 5 numbers
//then sort in ascending order
List <Integer> sorted = numbers.subList(0,5);
Collections.sort(sorted);
System.out.println("");
System.out.print("The winning numbers are: ");
//now pull the sorted numbers out and print
for (int j=0; j<5; j++)
{
System.out.print(sorted.get(j) + " ");
}
System.out.print(" and the powerball is: ");
//After shuffling, get the first powerball number
for (int k=0; k<1; k++)
{
System.out.print(powerball.get(k) + " ");
}
}
}
Couldn't resist throwing this together in Python:
import random
choices = [random.choice(range(1,60)) for i in xrange(5)]
choices.sort()
rchoices = " ".join(map(lambda x: str(x), choices))
print "The winning numbers are: %s" % rchoices
print " and the powerball is: %i" % random.choice(range(1,40))
Posted by: bmuller | April 08, 2009 at 12:17 PM
Behold the beauty of python.....I look forward to the first series of numbers generated by your code......
Posted by: Agricola | April 08, 2009 at 02:12 PM
these pages are of little, here I have discovered many things that really did not know, I thank you for the blog!these pages are of little, here I have discovered many things that really did not know, I thank you for the blog!
Posted by: generic viagra | May 17, 2010 at 05:28 PM
these pages are of little, here I have discovered many things that really did not know, I thank you for the blog!these pages are of little, here I have discovered many things that really did not know, I thank you for the blog!
Posted by: Round and Brown | July 05, 2010 at 06:55 AM
This info is very useful for me because I learn the code order phentermine
Posted by: Maxim Fesun | March 04, 2012 at 07:47 AM