Generating Random Numbers in Java

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/9

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

10 Terms

1
New cards

//What is the range that this call will generate?

System.out.println(Math.random());

[0, 1)

2
New cards

/**Suppose n is a correctly instantiated int variable

* What is the range that this call will generate?

*/System.out.println(Math.random() * n);

[0, n)

3
New cards

/**Suppose n is a correctly instantiated int variable

* What is the range that this call will generate?

*/

System.out.println(Math.random() + n);

[0 + n, 1 + n)

4
New cards

/**Suppose max and min are correctly instantiated int variables

* What is the range that this call will generate?

*/

System.out.println((Math.random()*(max - min)) + min);

[min, max)

5
New cards

/**What is the range that this call will generate?

* Pay attention to the casting call!

*/

System.out.println((int)(Math.random());

0

6
New cards

**Suppose n is a correctly instantiated int variable

* What is the range that this call will generate?

* Careful with the casting call!

*/

System.out.println((int)(Math.random() * n);

All integers from [0,n)

7
New cards

/**Suppose n is a correctly instantiated int variable

* What is the range that this call will generate?

* Careful with the casting call!

*/

System.out.println((int)(Math.random()) + n);

Every integer from [0 + n, 1 + n)

8
New cards

/**Suppose max and min are correctly instantiated int variables—

* What is the range that this call will generate?

* Careful with the casting call!

*/

System.out.println((int)(Math.random()*(max - min)) + min);

Every integer from [min, max)

9
New cards

What statement would you use to generate a random number from a minimum value, to a maximum value—exclusive?

(Math.random()*(max - min)) + min

10
New cards

What statement would you use to generate a random number from a minimum value, to a maximum value—inclusive?

(Math.random()*((max + 1) - min)) + min