在 Scala - Spark 中同时生成两个不同的随机数

Generating two different random number simultaneously in Scala - Spark

谁能帮我在两个范围内生成两个不同的随机数?我试过:

 var a = Random.nextInt(S)
 var b = Random.nextInt(K)
 if (a == S || b == K){
     a = S-1
     b = K-1
 }
 (word,a,b)

但这会生成一些不在指定范围内的数字。

来自docs on Random

def nextInt(n: Int): Int Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

根据方法契约,nextInt 将始终 return 一个从 0 到 n - 1 的值,因此您的条件 a == S || b == K 将始终为假。