guys im trying to create a simulation of the lottery using java... i am makin an array consisting of 5 random numbers ranging from 0 to 9 the thing is i can only get the amount of arrays the class has and everytime i run the application all i get for the 5 random numbers are a bunch of zeros.
this is my code so far...
import java.util.Scanner;
import java.util.Random;
public class Lottery
{
public static void main (String[] args)
{
int[] lotteryNumbers = new int[5];
int indx;
int num1, num2, num3, num4, num5;
Random rand = new Random();
int sum = 0;
Scanner key = new Scanner(System.in);
System.out.println("The array size is " + lotteryNumbers.length);
for (indx = 0; indx <5; indx++)
{
i forgot to mention the user also has to input his own 5 numbers to see if he won the "grand prize". but i think i have that part down for now... all i am really interested in are those damn 5 random numbers which i cant seem to get right... any help would be appreciated.
import random
a = [random.randint(0, 9) for i in xrange(10)]
print 'The size of the array is', len(a)
print a
It's not a professional enterprise scalable Web 2.0 AJAX XML-based business solution, but I wrote that in 20 seconds and it does what I believe you tried to do in >>1, only it works.
Name:
Anonymous2006-11-13 10:17
import random a = [random.randint(0, 9) for i in xrange(10)]
perl is superior:
@a=map{int rand 9}0..9;
Name:
Anonymous2006-11-13 12:40
>>12
Superior my ass, the only difference there is rand is pesting the default namespace and Perl lacks lists comprehensions, so you have to do shit with a less clear map and that 0..9 crap. Seriously, this syntax must have been thought of by a 12 year old. Larry Wall needs more Fisher Price, less coming up with crappy syntax.
Name:
Anonymous2006-11-13 14:13
just use Math.random() and remember to multiply by ten and mask as an int.
Name:
Anonymous2006-11-13 14:24
Seriously, this syntax must have been thought of by a 12 year old.
Worse; a Christian.
Name:
Anonymous2006-11-13 15:04
Perl lacks lists comprehensions, so you have to do shit with a less clear map and that 0..9 crap.
yeah, sure, emulating a map with that 'for i in xrange(10)' crap is a lot clearer than just using map in the first place.
and '0..9' seems a lot clearer to me than 'xrange(10)'...
>>1
Stop saying "create". That's asinine. It's like saying "get" when you mean "procure", or "take", or "become". The proper word in this instance is "write", as in programs or articles or books or fucking world4ch comments.
Name:
Anonymous2006-11-13 20:42
>>1
Ah, you have to generate the random numbers from a rule 30 cellular automata.
>>18
It's not asinine. It's like 'creating a story'. Works just as well as 'writing'.
Name:
Anonymous2006-11-14 6:36
>>16
List comprehensions don't emulate map. To do the same as a map is a small particular use (not at all the only one) of them. I agree that xrange is a bad name, but range is slower because it creates an actual list (xrange is a generator). As for 0..9, it's intuitive in a piece of text, but in a program, it's counter-intuitive, as you introduce yet another feature, yet another operator, yet another plethora of syntactic and precedence issues, etc.
Name:
Anonymous2006-11-14 8:20
>>21
Perl sucks, Python is a shitty ripoff of Perl with a couple of random features from Haskell. End of discussion.
Name:
Anonymous2006-11-14 9:37
>>13
There are many ways to do things in Perl. A good choice is to pick the most readable choise. Does this look better?
my @numbers;
push @numbers, int(rand(9)) for (1..5);
print "The size of the array is $#numbers\n", @numbers;
Name:
Anonymous2006-11-14 15:44
by the request, it's obvious this is for a class, in which case all these perl Vs python shit is moot.
OP, look through the Math class in the API, and just use Math.random()
Name:
Anonymous2006-11-14 16:22
using System;
class Lottery
{
static void Main()
{
int[] num = new int[5];
Random rand = new Random();
for (int i = 0; i < 5; i = i + 1)
{
while (num[i] == 0)
{
num[i] = rand.Next(9);
}
Console.WriteLine("Ball number " +i +" is " +num[i]);
}
Console.ReadLine();
}
}
It's in C# so I don't know how useful it will be for you.
class Lottery {
public static void main(String[] args) throws Exception {
int[] numbers = new int[5];
Random rand = new Random();
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("The array size is " + numbers.length);
// generate lottery ticket
for (int i = 0; i < numbers.length; i++) {
numbers[i] = rand.nextInt(9);
System.out.print(numbers[i]);
}
// get users ticket
System.out.print("\nEnter your numbers: ");
String user_ticket = br.readLine();
// check if they match
String winner_ticket = "";
for (int i = 0; i < numbers.length; i++) {
winner_ticket = winner_ticket + numbers[i];
}
if (winner_ticket.equals(user_ticket)) {
System.out.println("You won the grand prize!!");
}
else {
System.out.println("Someone on the internet did my homework :(");
}
}
}
Name:
Anonymous2006-11-14 18:18
else {SOP}
I lol'd
Name:
Anonymous2006-11-15 6:02
bump
Name:
Anonymous2006-11-17 7:03
Use Python faggot
Name:
Anonymous2010-09-21 13:14
Random class isn't really random. Also, Java sucks.