Randomize numbers excluding 3,7 out of {1,2,3,4,5,6,7,8,9,0}

Asked 2 years ago, Updated 2 years ago, 58 views

Hello, everyone How do I randomly pick a number out of a set of natural numbers?

java random

2022-09-22 15:52

1 Answers

Please refer to the sample below.

jshell> List<Integer> L = IntStream.range(0, 10).filter(i -> i !=3 && i != 7).boxed().collect(Collectors.toList());
L ==> [0, 1, 2, 4, 5, 6, 8, 9]

jshell> int choiceNumber = L.get(new Random().nextInt(L.size()));
choiceNumber ==> 5

jshell> int choiceNumber = L.get(new Random().nextInt(L.size()));
choiceNumber ==> 4

jshell> int choiceNumber = L.get(new Random().nextInt(L.size()));
choiceNumber ==> 1


2022-09-22 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.