473,396 Members | 1,671 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,396 software developers and data experts.

Random number

hello I am new to java very new and am hoping someone can offer me some
help.

I know that i need to use java.util.Random somewhere to generate a random
number

can anyone show me the code to do this very basic code to simply output the
random number that has been generated to the console?

I would really appreciate this, I can work from there to expand on what i
need ti to do

TIA

Christo

Feb 7 '07 #1
5 6200
Christo <ch************@hotmail.comwrote:
>can anyone show me the code to do this very basic code to simply output the
random number that has been generated to the console?
public class RandomCoin {
/** flip a coin. */
public static void main(String[] args) {
java.util.Random rand = new java.util.Random();
System.out.println(rand.nextBoolean() ? "heads" : "tails");
}
}

Of course, if you prefer some other random type than boolean, call the
appropriate method - see the javadoc.

If you're using it for cryptography, you might prefer SecureRandom.
--
Mark Rafn da***@dagon.net <http://www.dagon.net/>
Feb 7 '07 #2
This will create you a random double.

//Creates random double.
double random = Math.random();

//Turns double into a number >=1
double Random = random * 10;

//Cast the double as an int (removes decimals)
int xOry = (int)Random;

--
Regards JJ (UWA)

"Christo" <ch************@hotmail.comwrote in message
news:L6******************************@bt.com...
hello I am new to java very new and am hoping someone can offer me some
help.

I know that i need to use java.util.Random somewhere to generate a random
number

can anyone show me the code to do this very basic code to simply output
the random number that has been generated to the console?

I would really appreciate this, I can work from there to expand on what i
need ti to do

TIA

Christo
Feb 7 '07 #3
"j1mb0jay" <ja**@aber.ac.ukwrote in news:11***************@leri.aber.ac.uk:
This will create you a random double.

//Creates random double.
double random = Math.random();

//Turns double into a number >=1
No, because random may be a number less than 0.1
For example, 0.04382 * 10 becomes 0.4382

Perhaps you meant that this expands the range of the random number?
double Random = random * 10;

//Cast the double as an int (removes decimals)
....by truncation, may not be what you want. Consider Math.round().
int xOry = (int)Random;
There is some good information on generation of pseudorandom numbers in Java
at
http://mindprod.com/jgloss/pseudorandom.html

--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Feb 8 '07 #4
Sorry have only started messing with java when I started this course at
university, otherwise use to C#, was just trying to help, but if didn't know
it could return 0.0xxx I must edit code to allow for this mistake. Thanks
for the comment.

--
Regards JJ (UWA)

"Ian Shef" <in*****@avoiding.spamwrote in message
news:Xn****************************@138.126.254.21 0...
"j1mb0jay" <ja**@aber.ac.ukwrote in
news:11***************@leri.aber.ac.uk:
>This will create you a random double.

//Creates random double.
double random = Math.random();

//Turns double into a number >=1

No, because random may be a number less than 0.1
For example, 0.04382 * 10 becomes 0.4382

Perhaps you meant that this expands the range of the random number?
>double Random = random * 10;

//Cast the double as an int (removes decimals)

...by truncation, may not be what you want. Consider Math.round().
>int xOry = (int)Random;

There is some good information on generation of pseudorandom numbers in
Java
at
http://mindprod.com/jgloss/pseudorandom.html

--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Feb 8 '07 #5
Ian Shef wrote:
...by truncation, may not be what you want.
Usually truncation is what you want. Round is normally never what you want.
For instance to throw a dice, use 1+(int)(Math.random()*6).

Math.round(Math.random()*10) gives 0 with 5% probability, 10 with 5%
probability and 1-9 with 10% probability each.

-Rune
Feb 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.