![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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;
}
}
} |
|
|
|
|
|
#2 |
|
Programmer
|
what's with all of the for loops?
__________________
Free Porn!! |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
for (;;) = infinate loop |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I think he's saying, what's the point of them if you're breaking out on the first instance?
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2005
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
thanks. i just figures that out this morning
![]() |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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);
}
}
} |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Mar 2005
Posts: 17
Rep Power: 0
![]() |
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; 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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|