Results 1 to 7 of 7
- 12-15-2008, 08:14 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 15
- Rep Power
- 0
- 12-16-2008, 12:07 AM #2
Random()
The way to do this would be:
1- use a "for" loop
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
2- Within the "for" loop, generate the random number
Random (Java Platform SE 6)
Java Random Numbers
3- Populate the array with each random number generated:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Luck,
CJSL
PS:@carderne: please DO NOT suggest things you don't know anything about:
- RandInt method doesn't exist in Java
- The Integer class doesn't have a rand() method either.
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-16-2008, 04:17 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 25
- Rep Power
- 0
i done this before.
hope this may help you.
multiple with numbers will affect your resultJava Code:import java.util.*; int a = (int) (Math.random()*100);
*10 -> 0-9
*100 -> 0-99
because we want to round up the numbers, so we use int.
from double,we down cast it to int. (int)
PS:actually all this things can be found at any search engine.
(and that is not really hard to find the related source)
if you type "generate random numbers",
you may find some many related articles and sources.Last edited by angelicsign; 12-16-2008 at 04:20 AM.
- 12-16-2008, 02:36 PM #4
If you want to help...
@carderne: if you really want to help, then please put some effort into it. Sending someone on a wild goose chase is not very helpful. If you can't remember what the name of the method is, then take the effort look it up. Integer.rand() is in no way or form even close to math.random().
We all appriaciate any help we can get, but it has to be the right help.
Thanks,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-16-2008, 03:46 PM #5
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
You're right and I apologise. My only excuse being that I was on my phone with no access to a computer...
Unruly posts duly removed.
- 12-16-2008, 03:54 PM #6
Thanks
Apology accepted. We're in sync. Thanks for the help.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-21-2010, 10:40 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
Generating random numbers
Hi this is the code to generate 5 random numbers and then put them into an array. If the same number is produced twice then the second one is ignored and then the procedure repeated. Hope this helps. To generate the random number up to 99 use Math.random()*100, which is equal to 0 - 100-1 which is 99.
public class RandomArray
{
public static void main(String[] args)
{
//This is the array to hold the random numbers
int [] numbers = new int [5];
//This is the variable that will hold the random number each time it is created
int num;
//This boolean will hold the value if there is a number being put into the array. If there is then it cancels
//the first do while loop and then creates the next random number
boolean numberIn = false;
//This is the variable that is incremented each time there is a number put into the array. This also cancels the outer do while loop
//when the count is more than numbers.lenght-1 which is greater than 4. The indexes in the array range from 0-4
int count;
//This loop is used to read in -1 to each element in the array. This is because the element is filled by 0's by default
//If we dont prefill it then it makes it harder if one of the random numbers to be put into the array is 0
for (int loop = 0; loop<numbers.length; loop++)
{ numbers[loop]=-1;
System.out.println(numbers[loop]);
}//for
//This outer do while is to generate the random number. When it is created count is initialised to 0 and NumberIn false
//Then the inner loop goes through each index in the array to see if it is -1 which means no number has been put into that
//index. If the number is not -1 and it is not the same as the number we are trying to put into the array then we go to
//the next index in the array and see if its empty. This process is repeated until all the array indexes are full and then ends
//the process
do
{
num =(int)( Math.random()*10);
count = 0;
numberIn = false;
do
{
//If the value in the array index is the same as the number trying to put in then exit the loop to generate another number
if (numbers[count]==num)
numberIn = true;
//If the array index is empty(It is -1 which we prefilled the array with) put the number in and then exit this loop
else if (numbers[count]==-1)
{ numbers[count]=num;
numberIn = true;
}//else if
else
//Otherwise go to the next index in the array and then start the inner do while again
count++;
}while(!numberIn);//While we have not put a number into the array continue
}while(count!=4);//while the array index has not reached the last one
//print out the index of the array just to test and see the values
for (int trip = 0; trip<numbers.length; trip++)
System.out.println(numbers[trip]);
Similar Threads
-
Store String Tokenizer o/p onto an array
By rajchief in forum New To JavaReplies: 1Last Post: 08-08-2008, 09:00 PM -
Can I store multiple objects in an array
By lareauk in forum New To JavaReplies: 9Last Post: 05-29-2008, 03:57 AM -
How would you get information from a file and then store it in an array?
By szimme101 in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 06:02 PM -
questions about using array to store profile
By hien_NU in forum New To JavaReplies: 6Last Post: 01-08-2008, 05:03 AM -
Create a VeryLong class that will store an integer of arbitrary length.
By hey in forum New To JavaReplies: 2Last Post: 12-12-2007, 05:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks