我是靠谱客的博主 高挑小海豚,这篇文章主要介绍使用Scala生成随机数,现在分享给大家,希望可以做个参考。

一.使用Scala生成随机数

1.简单版本:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 1.you can use scala.util.Random.nextInt(10) to produce a number between 1 and 10 2.at the same time,you nextInt(100) to produce a number between 1 and 100 */ object Test { def main(args: Array[String]) { var i = 0 while(i < 10) var str = scala.util.Random.nextInt(100).toString println(str) i = i+1 } } }

这里写图片描述
这里写图片描述
2.复杂版本:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
object Test{ def main(args: Array[String]): Unit = { val wordPerMessage = 4 var i = 0 while(i<10){ /* 1.the (1 to 1) is meaning that only have one circulation. */ (1 to 1).foreach { messageNum => { //[There's only three cycle] val str: Seq[String] = (1 to wordPerMessage).map(x => scala.util.Random.nextInt(10).toString) val str1 = str.mkString(" ")//separate str1 with space println(str) } } i = i +1 } } }

最后

以上就是高挑小海豚最近收集整理的关于使用Scala生成随机数的全部内容,更多相关使用Scala生成随机数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部