Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2005, 9:45 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Random Numbers

Hey guys i need u to see if this program is runnign properly. I want it to show 20 random numbers between 5 and 10. Mutiple numbers allowed. If u think something is wrong, please let me know
import java.awt.*;
import hsa.Console;
public class random
{
    static Console c = new Console (5, 20);
    static public void main (String[] args)
    {
        int x, y, z, a, b;
        int x1, x2, x3;
        int y1, y2, y3;
        int z1, z2, z3;
        int a1, a2, a3;
        int b1, b2, b3;
        c.clear ();
        for (;;)
        {
            x = (int) (Math.random () * 5) + 5;
            c.print (x);
            break;
        }
        for (;;)
        {
            x1 = (int) (Math.random () * 5) + 5;
            c.print (x1, 5);
            break;
        }
        for (;;)
        {
            x2 = (int) (Math.random () * 5) + 5;
            c.print (x2, 5);
            break;
        }
        for (;;)
        {
            x3 = (int) (Math.random () * 5) + 5;
            c.print (x3, 5);
            break;
        }
        for (;;)
        {
            y = (int) (Math.random () * 5) + 5;
            c.print (y, 5);
            break;
        }
        for (;;)
        {
            y1 = (int) (Math.random () * 5) + 5;
            c.print (y1, 5);
            break;
        }
        for (;;)
        {
            y2 = (int) (Math.random () * 5) + 5;
            c.print (y2, 5);
            break;
        }
        for (;;)
        {
            y3 = (int) (Math.random () * 5) + 5;
            c.print (y3, 5);
            break;
        }
        for (;;)
        {
            z = (int) (Math.random () * 5) + 5;
            c.print (z, 5);
            break;
        }
        for (;;)
        {
            z1 = (int) (Math.random () * 5) + 5;
            c.print (z1, 5);
            break;
        }
        for (;;)
        {
            z2 = (int) (Math.random () * 5) + 5;
            c.print (z2, 5);
            break;
        }
        for (;;)
        {
            z3 = (int) (Math.random () * 5) + 5;
            c.print (z3, 5);
            break;
        }
        for (;;)
        {
            a = (int) (Math.random () * 5) + 5;
            c.print (a, 5);
            break;
        }
        for (;;)
        {
            a1 = (int) (Math.random () * 5) + 5;
            c.print (a1, 5);
            break;
        }
        for (;;)
        {
            a2 = (int) (Math.random () * 5) + 5;
            c.print (a2, 5);
            break;
        }
        for (;;)
        {
            a3 = (int) (Math.random () * 5) + 5;
            c.print (a3, 5);
            break;
        }
        for (;;)
        {
            b = (int) (Math.random () * 5) + 5;
            c.print (b, 5);
            break;
        }
        for (;;)
        {
            b1 = (int) (Math.random () * 5) + 5;
            c.print (b1, 5);
            break;
        }
        for (;;)
        {
            b2 = (int) (Math.random () * 5) + 5;
            c.print (b2, 5);
            break;
        }
        for (;;)
        {
            b3 = (int) (Math.random () * 5) + 5;
            c.print (b3, 5);
            break;
        }
    }
}
Dark Flare Knight is offline   Reply With Quote
Old Apr 6th, 2005, 12:07 AM   #2
gamesage
Programmer
 
Join Date: Sep 2004
Location: Clemson, SC
Posts: 35
Rep Power: 0 gamesage is on a distinguished road
Send a message via AIM to gamesage
what's with all of the for loops?
__________________
Free Porn!!
gamesage is offline   Reply With Quote
Old Apr 6th, 2005, 3:48 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
for (;;) = infinate loop
Berto is offline   Reply With Quote
Old Apr 6th, 2005, 4:26 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I think he's saying, what's the point of them if you're breaking out on the first instance?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 6th, 2005, 4:32 PM   #5
Childe Roland
Newbie
 
Childe Roland's Avatar
 
Join Date: Mar 2005
Posts: 17
Rep Power: 0 Childe Roland is on a distinguished road
your code but there a few changes you should make to it.
the way it is set up it the highes number it will generate it a 9.
also you really don't need all those seperate for ( ;;; ) statements and 20 different variables you can do the same thing with just one variable and one for loop
like i did below.
import java.awt.*;
import hsa.Console;

public class Random1
{
	static Console c = new Console (5, 20);
	public static void main (String [] args)
	{ 
		int x;
 
 		x = (int) (Math.random () * 6) + 5;
 		c.print (x);
 
 		for(int i = 1; i < 20; i++)
   		{
	  			x = (int) (Math.random () * 6) + 5;
	  			c.print (x ,5);
			}
	}
}

Last edited by Childe Roland; Apr 6th, 2005 at 4:35 PM.
Childe Roland is offline   Reply With Quote
Old Apr 7th, 2005, 4:12 PM   #6
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
thanks. i just figures that out this morning
Dark Flare Knight is offline   Reply With Quote
Old Apr 8th, 2005, 4:40 AM   #7
Gigabytee
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 Gigabytee is on a distinguished road
Question

hay,
I know the question is well answered but i had another (subqustion)
how did u limit the Random Numbered (under 10)
I would normally add a while loop there "while(x>9)" or something like that...

public class Random1
{
	public static void main (String [] args)
	{ 
		int x=10;
 		for(int i = 0; i < 20; i++)
   		{
   				x = (int) (Math.random () * 8) + 4;
	  			while(x>9)
	  				x = (int) (Math.random () * 8) + 4;
	  			System.out.println(x);
			}
	}
}
Gigabytee is offline   Reply With Quote
Old Apr 8th, 2005, 12:23 PM   #8
Childe Roland
Newbie
 
Childe Roland's Avatar
 
Join Date: Mar 2005
Posts: 17
Rep Power: 0 Childe Roland is on a distinguished road
Cool

you could do it the way you are doing but it could take a while to actually get a number that is less then 10 sometimes. it maybe better to change this part
(int)(Math.random () * 8) + 4;
the way it is now it generates numbers between 4 and 11

if you wanted them to stay under 10 try
(int)(Math.random () * 7) + 4

Last edited by Childe Roland; Apr 8th, 2005 at 12:27 PM.
Childe Roland is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC