answersLogoWhite

0


Best Answer

Take advantage of Java's easy-to-use Random class.

// Create a new Random object.

// The constructor accepts a single Long argument.

// This is the seed for the random generator.

// Using the current time is standard for most applications.

Random rnd = new Random(System.currentTimeMillis());

// A call to nextInt(n) will generate a random value from 0 to n-1

// This is typical in programming languages, and in order to get a specific range we need

// to tweak it a bit.

rnd.nextInt(n);

// This will give you a random int from (start) to (start + range - 1)

rnd.nextInt(range) + start;

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

For random numbers, you either use Math.random() or - for more options, including a minimum and maximum - methods of the Random class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you generate a random integer within a range in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What does generate random numbers mean?

It means to get a number, randomly, from a certain range of numbers.It means to get a number, randomly, from a certain range of numbers.It means to get a number, randomly, from a certain range of numbers.It means to get a number, randomly, from a certain range of numbers.


Generate N random numbers in between 1 and 6 inclusive?

// First we want to create our random number generator. // Normally we seed it with the current system time, but go ahead // and use another method if you don't like that. Random rnd = new Random(System.currentTimeMillis()); // Next step is to create a place to put our numbers. // (Assuming you want to generate random integers) int[] nums = new int[N]; // Now fill our array with random numbers. // Random.nextInt(n) will give us a number from 0 (inclusive) to n (exclusive)... // So we want to first get a number from [0-6) and add 1 to get the specified range. for( int i = 0; i < nums.length; ++i ) nums[i] = rnd.nextInt(6) + 1;


How do you generate palindrome number between the given range?

The generation of palindromic numbers within a given range is best done with a computer program. Space it limited so an example of program code cannot be shown here, but the Codecast website gives full guidance.


How do you import random in python 3.2?

import random random.randrange(1, 99999) #Set range from 1 to 99999


What is short integer?

In computer programming, a variable can be (among other things) an integer or a long integer. An integer can be any whole number in the range of -32,768 to 32,767 A long integer can be any whole number in the range of -2,147,483,648 tp 2,147,483,647 I have never heard of an "integer" variable being called a "short integer" but it makes a kind of sense. Note: The size of integer types is platform-dependent, but usually: short: 16 bits int: 32 bits (16 in archaic systems: MSDOS OS Windows16) long: 32 bits (64 in unix64) long long: 64 bits

Related questions

Random variable that can take any numeric value within a range of values?

continuous random variable


Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7?

In C, one possible solution: int rnd7 () { int i, r; r = 0; for (i = 0; i < 9; i++) r += rnd5() - 1; return 1 + (r % 7); }


What would the integer be of a rolled number cube?

It could be a random variable with a discrete uniform distribution over the range 1 to 6.


How do you write C program to print a Series of Odd Numbers and Even Numbers.?

Generate a random number in half the range you are interested in. If generating odd values, subtract 1 from the upper bound of the range. That is, if the range is 0 to 100, then generate a random number in the range 0 to 50 for even numbers and 0 to 49 for odd numbers. Double the generated number to obtain the even value, or double it and add 1 to obtain the odd value.


What is a randbetween in a spreadsheet?

Randbetween allows you to generate random numbers with a range. You specify the lowest and highest that you want the number to be between. So if you wanted to generate a random number between 1 and 100, you would enter: =RANDBETWEEN(1,100) If you wanted to generate a random number between 25 and 60, you would enter: =RANDBETWEEN(25,60) The function will generate a new random number every time you make a change to the spreadsheet or press the F9 key. So if you want to retain the random numbers it generates, you could copy them and then do a Paste Special and choose Values. You could paste this on top of the original values or in a new location.


What does generate random numbers mean?

It means to get a number, randomly, from a certain range of numbers.It means to get a number, randomly, from a certain range of numbers.It means to get a number, randomly, from a certain range of numbers.It means to get a number, randomly, from a certain range of numbers.


What is probability distibution?

A statistical function that describes all the possible values and likelihoods that a random variable can take within a given range.


Can an integer repeat in the range?

Yes.


What is the signed integer range?

The signed integer range extends only from negative infinity to positive infinity. You have to make up your own names and symbols for whole numbers that are not included in that range.


Given a function which produces a random integer in the range 1 to 5 write a function which produces a random integer in the range 1 to 7?

import java.util.Random;public class Test{static int rand7() {int i;int max;i = 5 * rand5() + rand5();max = 25;while(i < max%7) {i *= 5;i += rand5();max %= 7;max *= 5;}return(i%7);}private static int rand5(){Random rand = new Random() ;int min = 1;int max = 5;int randomNum = rand.nextInt(max - min + 1) + min;return randomNum;}public static void main(String [] args){System.out.println(rand7());}}


What is the difference between signed integer and unsigned integer in terms of memory and range?

Signed integer is any integer that carries negative sign while unsigned integer is any integer that carries positive sign


What does integer range mean?

The problem must have given you a list of several whole numbers. The integer range is the difference in size (distance) between the biggest one and the smallest one.