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)
So simply multiply a random number from 1 to … and this one with 3. Ready.
That is true, and there is obviously to be limited to 300 here(?) there is no risk of overflow.
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().