Home arrow static arrow Java Programming [Archive] - Generate random number from set array
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Generate random number from set array
This topic has 12 replies on 1 page.

Posts:11
Registered: 8/4/04
Generate random number from set array  
Aug 4, 2004 2:27 PM



 
Hi,

How could you best write a single statement that will print a number at random from the set:
a ) {2,4,6,8,10};
b) {3,5,7,9,11};
c) {6,10,14,18,22};

Thanks for the help!

Quinton.
 

Posts:31,095
Registered: 4/30/99
Re: Generate random number from set array  
Aug 4, 2004 2:32 PM (reply 1 of 12)



 
There are three sets there, so the question doesn't make sense.
 

Posts:11
Registered: 8/4/04
Re: Generate random number from set array  
Aug 4, 2004 2:33 PM (reply 2 of 12)



 
Ooops...i meant from each set...
 

Posts:6,750
Registered: 1/25/04
Re: Generate random number from set array  
Aug 4, 2004 2:36 PM (reply 3 of 12)



 
Why do you want to do that with a single statement?
 

Posts:2,206
Registered: 8/15/02
Re: Generate random number from set array  
Aug 4, 2004 2:37 PM (reply 4 of 12)



 
Random r = new Random(); //aint e = 2*(r.nextInt(5)+1); //bint o = ( 2*(r.nextInt(5)+1) +1; //cSystem.out.println("Do your homework yourself");
 

Posts:11
Registered: 8/4/04
Re: Generate random number from set array  
Aug 4, 2004 2:42 PM (reply 5 of 12)



 
i guess like when you print random #'s from 1-4 you do
Math.random()*4;

but here in my current problem....you have to draw from these Odd or even #'s in a certain range...

so I guess without the restrictions on printing Odd or Even #'s..you would say

variable = Math.random()*10;// to print all #'s from 0-10....

So i guess I understand the Question more now...how would you select Random EVEN or ODD #'s in a certain range?
 

Posts:3,534
Registered: 9/30/99
Re: Generate random number from set array  
Aug 4, 2004 2:48 PM (reply 6 of 12)



 
Each of your sets have numbers that can be considered in positional values, put them in arrays and reandomly generate an index to call the contents of one of your array elements back. If you need to choose the set randomly then make it a 2 dimensional array and gererate 2 numbers randomly.
 

Posts:5,965
Registered: 5/17/03
Re: Generate random number from set array  
Aug 4, 2004 2:50 PM (reply 7 of 12)



 
So i guess I understand the Question more now...how
would you select Random EVEN or ODD #'s in a certain
range?

If x is any integer then 2*x will be even and 2*x+1 will be odd.

x = 1,2,3, 4, 5
2*x = 2,4,6,8,10
2*x+1 = 3,5,7,9,11

You generate x randomly and calculate 2*x and 2*x+1 as EvilEdna did.

 

Posts:2,206
Registered: 8/15/02
Re: Generate random number from set array  
Aug 4, 2004 3:14 PM (reply 8 of 12)



 
If yuoe really want to use Math.random...
//aint e = 2* (((int)(Math.random()*5) +1);


Someone better than me can tell you whether, due to rounding problems, (int)(Math.random()*5) could ever return 5, but I suspect not. If it did, then you could get the very occasional value 12 coming out.

The rest is up to you
 

Posts:11
Registered: 8/4/04
Re: Generate random number from set array  
Aug 4, 2004 3:24 PM (reply 9 of 12)



 
Do I still have to add C in there? Its no fun to do homework by yourself :P
 

Posts:2,206
Registered: 8/15/02
Re: Generate random number from set array  
Aug 4, 2004 3:27 PM (reply 10 of 12)



 
You have everyhting you need

1. All the number are even
2. The increment is 4
3. the max allowed is 22

From these three facts and the information already given, you should be able to construct a 1liner using either Random or Math.random()
 

Posts:2,206
Registered: 8/15/02
Re: Generate random number from set array  
Aug 4, 2004 4:54 PM (reply 11 of 12)



 
Do I still have to add C in there? Its no fun to do
homework by yourself :P

This was too good a chance to miss....
int even = 2* ((int)( ((double)random()/RAND_MAX) *5) + 1);\


Just remember to include stdlib.h
 

Posts:11
Registered: 8/4/04
Re: Generate random number from set array  
Aug 5, 2004 11:35 AM (reply 12 of 12)



 
thanks a lot guys....sorry i couldn't reply sooner...but i was stuck in class from 4pm last night..
 
This topic has 12 replies on 1 page.