Zufällige zahl von 3, 6, 9, 12…?

Hallo,

Ich habe Informatik in der Schule und eine Aufgabe bei der ich zufällige zahlen “würfeln” soll die aber nicht direkt beieinander liegen sondern immer +3 also zahlen 3, 6, 9, 12, 15, 18, … 300. weiß jemand wie das funktioniert? (In Java)

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Babelfish
1 year ago

So simply multiply a random number from 1 to … and this one with 3. Ready.

jo135
1 year ago
Reply to  Babelfish

That is true, and there is obviously to be limited to 300 here(?) there is no risk of overflow.

jo135
1 year ago

I’m not sure if I understand the task. So you’re supposed to determine random numbers that all have to be divided by three?

For example, you can simply cube until the random number can be divided by 3 (the Modulo operator lets you check this easily).

As described by Babelfish: if the numbers are to go only up to 300 (there is no clear indication), you can naturally easily generate a random number of 1 to 100 and multiply with 3 without risk.

Full number of random numbers Random.nextInt().