简体   繁体   中英

Generating two different random number simultaneously in Scala - Spark

Can anybody help me generate two different random numbers in two ranges? I've tried:

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

But this generates some numbers that are not in the specified ranges.

From the 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.

By the method contract, nextInt will always return a value from 0 to n - 1 , so your condition a == S || b == K a == S || b == K will always be false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM