Results 1 to 11 of 11
Thread: [SOLVED] Help with math.random
- 12-21-2008, 12:44 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
[SOLVED] Help with math.random
Hi,
how to use math.random so i can generate random numbers from an already created array.
for example:
Lets say i create an array of 10 numbers
How to use math.random to generate only 3 random numbers from this 10??PHP Code:int[] numbers = {22,34,122,44,23,90,30,50,1,2};
Thanks in advance for your answers
tomiu
-
One way to do this is use an ArrayList:
Java Code:import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Fubar { public static void main(String[] args) { int[] numbers = {22,34,122,44,23,90,30,50,1,2}; List<Integer> intList = new ArrayList<Integer>(); for (int i = 0; i < numbers.length; i++) { intList.add(numbers[i]); } Collections.shuffle(intList); int max = 3; for (int i = 0; i < max; i++) { if (intList.size() > 0) { int foo = intList.remove(0); System.out.println(foo); } } } }
- 12-21-2008, 04:07 PM #3
You can also use the Random class
You can also use the Random class:
Random (Java Platform SE 6)
Example:
Java Practices -> Generate random numbers
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-21-2008, 05:25 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
Fubarable since i'm beginner i needed some time to understand your code...but when i compile it i get this error:
PHP Code:Fubar.java:12: '(' or '[' expected List<Integer> intList = new ArrayList<Integer>(); ^ 1 error
CJSLMAN my problem with the Random class is that i can only generate random numbers in generall(from 1 to 1000, or random numbers between 40 and 50 etc).
- 12-21-2008, 05:34 PM #5
Huh?
I don't think I understood your comment. I also don't think you understand how the Random class works. You can seed it using the nextInt method (see the link of the example). For example:CJSLMAN my problem with the Random class is that i can only generate random numbers in generall(from 1 to 1000, or random numbers between 40 and 50 etc).
This will return a random number between 0-10. Put it in a "for" loop and you'll get 3 random numbers.Java Code:Random rnd = new Random(); int randomNumber = rnd.nextInt(11);
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-21-2008, 05:42 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
- 12-21-2008, 05:53 PM #7
ah... that's an animal of a different color...
ah, OK, well, FYI, you'll have this same issue if you use math.random()...
So part of the solution is done (you can create random numbers). Now you need to go to the next step: what do you do with those numbers? I'm going to assume that you do not know the basics of an array (how to get and set elements). Go study this link:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
When you understand athe basics of arrays, the random numbers are the index numbers into the array (not the elements themseves).
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-21-2008, 06:15 PM #8
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
Hi CJSL,
i know that the random numbers are the index numbers, i just dont know how to use math.random with the index numbers(allmost no examples for this on the net, i'm looking all day)...
this is what i have done so far:
With this code i can generate random numbers from 1-45(also i can delete duplicate results)...But if i create an array and put some element, than i dont know how to use random...PHP Code:for(int i = 0; i<Loto1.length;i++){ Loto1[i]=1+(int)(45*Math.random()); for (int j = 0; j < i; j++){ if(Loto1[i] == Loto1[j]){ Loto1[i]-=1; } } }
- 12-21-2008, 06:23 PM #9
Pseudo code
It would look something like this:
Java Code:- Create array - Intiatiate Random class - Do "for" loop three times { - Generate random index between 0 and 10 (or whatever number you want) - Use generated random number as index for the array (int number = array[random number]) - Print number or put in an array or something }Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-21-2008, 07:13 PM #10
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
thx for ur help CJSLMAN :)..it took me some time but now i get the result i want...
PHP Code:import java.util.Random; public class Lotol1 { public static void main (String args[]) { int Loto[] = {1, 22, 25, 40, 50, 4, 5, 15}; Random random = new Random(); int temp=0; int i=0; int j=0; for (; i<Loto.length;i++) { } for(; j<3; j++) { temp=random.nextInt(7)+0; int number = Loto[temp]; System.out.println(" " + number); if(Loto[j]==Loto[temp]) { Loto[j]-=1; } } } }
- 12-21-2008, 09:55 PM #11
Welcome
Glad to help... just one obeservation:
The above code will only randomly produce index numbers from 0 - 6 and you're array has 8 elements, therefore the index range should be 0-7. Probably should use random.nextInt(8)Java Code:temp=random.nextInt(7)+0;
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Array of instances using Math.random()
By xgi1008 in forum New To JavaReplies: 16Last Post: 01-25-2011, 11:10 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Formatting isbn number with Math.random()
By dns77x7 in forum New To JavaReplies: 11Last Post: 09-21-2008, 06:02 PM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 02:09 PM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks