Home arrow static arrow Java Programming [Archive] - Parsing random integers into a Strings
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Parsing random integers into a Strings
This topic has 7 replies on 1 page.

Posts:114
Registered: 7/5/04
Parsing random integers into a Strings  
Jul 18, 2004 3:11 AM



 
Hi

Its kind of simple question thu but its so hard for me,

Im using the Random class to generate randomize numbers but my applicatiom must those numbers as a String and not integers. How can I parse the returned random int into a String?

String label = random.nextInt(70).toString(); ????

Thanks
 

Posts:197
Registered: 02.09.97
Re: Parsing random integers into a Strings  
Jul 18, 2004 3:23 AM (reply 1 of 7)



 
String label = String.valueOf(random.nextInt(70));
 

Posts:114
Registered: 7/5/04
Re: Parsing random integers into a Strings  
Jul 18, 2004 3:28 AM (reply 2 of 7)



 
Thanks a lot!
 

Posts:114
Registered: 7/5/04
Re: Parsing random integers into a Strings  
Jul 18, 2004 6:38 AM (reply 3 of 7)



 
Hi

one more qestion,how can i set the random numbers to be in a specefied range?

gen.nextInt(1-70)?

Thanks
 

Posts:18,384
Registered: 21.03.00
Re: Parsing random integers into a Strings  
Jul 18, 2004 6:44 AM (reply 4 of 7)



 
gen.nextInt(70) + 1;

nextInt(n) gives an integer in the range of 0 to n-1, so nextInt(70) +1 will give you an integer in the range of 1 - 70.

/Kaj
 

Posts:2,821
Registered: 7/10/01
Re: Parsing random integers into a Strings  
Jul 18, 2004 6:45 AM (reply 5 of 7)



 
rnd.nextInt(maximum-minimum)+minimum;
 

Posts:11,200
Registered: 7/22/99
Re: Parsing random integers into a Strings  
Jul 18, 2004 6:46 AM (reply 6 of 7)



 
The nextInt(int) method returns a random number between 0 and the passed number, excluding the passed number. From this you get any range by adding or subtracting numbers.

Like to get a number from 1 to 70, first generate a number from 0 to 69 and add one. In code, "1+gen.nextInt(70)"
 

Posts:114
Registered: 7/5/04
Re: Parsing random integers into a Strings  
Jul 18, 2004 7:38 AM (reply 7 of 7)



 
Thanks guys!
 
This topic has 7 replies on 1 page.