User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,610 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,507 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2351 | Replies: 2
Reply
Join Date: Oct 2007
Posts: 3
Reputation: paydfody is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paydfody paydfody is offline Offline
Newbie Poster

Random Number Java Code

  #1  
Nov 11th, 2007
Hello guys, Hope ur listening. I need my code to generate 6 random numbers for each line, instead of just 1. Furthermore, there should be no duplicate numbers on any particular line. So far I have gotten this code to print only a single number per line, so I need to add 5 more numbers to each line. Thanks in advance.import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Lottotest
{

private static List lottoBarrel = new ArrayList(45);

private static void initializeBarrel()
{
for (int i = 1; i < 45; i++)
{
lottoBarrel.add(new Integer(i));
}
}

private static void printBarrel(String message)
{
System.out.println(message + "\n");
for (int i = 1; i <= 6; i++)
{
System.out.println("number " + i +" = " + lottoBarrel.get(i));
}
System.out.println("============");
}

public static void main(String[] args)
{
initializeBarrel();
Collections.shuffle(lottoBarrel);
printBarrel("Barrel rolled:");
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Posts: 74
Reputation: lookof2day is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Random Number Java Code

  #2  
Nov 11th, 2007
U should use java.util.Random or Math.random() to generate random numbers.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Random Number Java Code

  #3  
Nov 11th, 2007
1) you're not generating any random numbers at all. You're drawing your numbers from a collection of fixed numbers which has been ordered using a random sequence. That's not the same thing (but it's likely you want in this case).
2) you're explicitly printing a line terminator after every number when you call println, which is why you see your non-random numbers each on a line of their own.
3) you're not using code tags.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC