473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Generating Random Numbers

Im trying to generate random numbers using the statement:

Random r = new Random(2);
for (int i=1;i<=18;i++)

but im having errors in my results here is my entire code so far:

import java.util.*;
public class Random4
{
public static void main(String []args)
{
Random r = new Random(2);
for (int i=1;i<=18;i++)

{
int n=r.nextInt(19);

switch(n)
{
case 1:
System.out.println("n is a multiple of 3");
break;
case 2:
System.out.println("n is a multiple of 4");
break;
case 3:
System.out.println("n is a multiple of 5");
break;
default:
System.out.println("Chill dude");
break;
}

System.out.println(n);




}
}
}

Whats wrong with it?
Aug 1 '07 #1
2 1526
iam_clint
1,208 Expert 1GB
Wrong area moving you over to java area.
Aug 1 '07 #2
blazedaces
284 100+
Im trying to generate random numbers using the statement:

Random r = new Random(2);
for (int i=1;i<=18;i++)

but im having errors in my results here is my entire code so far:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Random4
  3. {
  4.     public static void main(String []args)
  5.     {
  6.         Random r = new Random(2);
  7.         for (int i=1;i<=18;i++)
  8.  
  9.         {
  10.             int n=r.nextInt(19);
  11.  
  12.             switch(n)
  13.             {
  14.             case 1: 
  15.                 System.out.println("n is a multiple of 3"); 
  16.                 break;
  17.             case 2: 
  18.                 System.out.println("n is a multiple of 4"); 
  19.                 break;
  20.             case 3:
  21.                 System.out.println("n is a multiple of 5"); 
  22.                 break;
  23.             default:   
  24.                 System.out.println("Chill dude");
  25.                 break;
  26.             }
  27.  
  28.                 System.out.println(n);
  29.  
  30.  
  31.  
  32.  
  33.         }
  34.     }
  35. }
Whats wrong with it?
Hello, and welcome to TSDN. First let's go over a few basics that will help you get the help you need:

1. Use code tags please. See how right above me the code looks a little nicer, well when you post look to the right to a box that says "REPLY GUIDELINES" and it will explain to you how to use code tags.

2. When you mention there are errors, it's helpful to tell us what those errors are, because often this is the biggest clue to figure out what's wrong with the program.

On a last note, I'm not 100% sure but I don't know why you're setting the "seed" of your random variable to be 2 when you do "Random r = new Random(2)" (look at the documentation of the random class here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html ).

That might be your issue, but then again I don't know what you want and what is showing up.

Good luck,
-blazed
Aug 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.