Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to generate random number in Scala

2 Answers

0 votes
var number: Int = 0;
var i: Int = 0;
val r = new scala.util.Random

while (i < 10) {
	number = r.nextInt();
	print(s"$number\n");
	i += 1;
}



/*
run:

2086227637
-256484989
-478324221
-190837632
366914910
1714083011
-1915596327
-887259382
598797977
814669133

*/

 



answered Oct 14, 2021 by avibootz
0 votes
def random_between_range(first: Int, last: Int): Int = {
	val r = new scala.util.Random
		
	var number: Int = first + r.nextInt((last - first) + 1);

    return number;
}
	
var i: Int = 0;
while (i < 20) {
	println(random_between_range(25, 33));
	i += 1;
}





/*
run:

33
32
33
25
28
25
33
29
28
28
26
26
26
26
25
25
25
29
25
33

*/

 



answered Oct 14, 2021 by avibootz
edited Oct 14, 2021 by avibootz
...