kotlin Get random string from array

kotlin random string
generate 4 digit random number in kotlin
select random string from array java
kotlin random double in range
random array kotlin
kotlin generate unique id
kotlin random hex
kotlin nextint

New to kotlin and quite a few questions and answers, mostly in Java. After following the documentation and verifying against numerous SO questions/answers Android Studio is complaining about the following in a function. The function is

fun getRandomQuote() {
    val randomValue = Random.nextInt(quotes.size)
    //return quotes[randomValue]
    return quotes.get(randomValue)
}

quotes is an arrayOf(strings)

val quotes = arrayOf("Alert today – Alive tomorrow.",...)

It says for

quotes.get(randomValue)

That it should be a Unit but found String. randomValue should be an integer as defined by the docs using nextInt unless I misinterpreted the docs. I don't see the issue. I'm just trying to randomly return a string from the array. I thought maybe another false positive from studio so I cleaned the project but it stops here on building. Anyone see what it is I'm doing wrong.

Your function doesn't specify a return type, that's why the compiler is expecting 'Unit' which would be the same as void in Java.

When you want to return a String, you need to specify it like so

fun getRandomQuote(): String {
    val randomValue = Random.nextInt(quotes.size)
    return quotes[randomValue]
}

See the docs for Kotlin functions as a reference.

It is also more kotliny to use indexing for arrays with square brackets instead of get() function.

Generate a Random Alphanumeric String in Kotlin, Learn a few handy ways to generate a random alphanumeric string. to generate a random alphanumeric String in Kotlin using three different approaches: Java Random, Kotlin val bytes = ByteArray(STRING_LENGTH). An abstract class that is implemented by random number generator algorithms. The companion object Random.Default is the default instance of Random.. To get a seeded instance of random generator use Random function.

As the other answers said, your function is missing return type, so by default it's Unit. Specifying the return type as String fixes the error.

I'd like to propose another more succinct way to choose a random element from an array using the default random number generator: just use the random extension function available on collections and arrays.

    val colors = arrayOf("red", "blue", "yellow")
    val randomColor = colors.random()

random, @ExperimentalUnsignedTypes fun UShortArray.random( random: Random ): UShort. Returns a random element from this array using the specified source of� An array is a collection of similar data types either of Int, String, etc. Array in Kotlin is mutable in nature with fixed size which means we can perform both read and write operations, on the elements of an array. Arrays in Kotlin are able to store multiple values of different data types.

You are just missing this

 fun getRandomQuote(): String {
     val randomValue = Random.nextInt(quotes.size)
     //return quotes[randomValue]
     return quotes.get(randomValue)
 }

Since you are returning from the function which is a String. If we don't return anything, it means its Unit in Kotlin and we don't have to mention it and return it. Other than that we got to mention what we are returning by putting right to the colon. ex:

fun getRandomQuote(): String{
  //something goes here
}

Random, To get a seeded instance of random generator use Random function. ByteArray. Fills the specified byte array with random bytes and returns it. open fun � Represents an array (specifically, a Java array when targeting the JVM platform). Array instances can be created using the arrayOf, arrayOfNulls and emptyArray standard library functions. See Kotlin language documentation for more information on arrays.

Kotlin Beginner, What I would like to do is generate a random number within a range, which I think would allow me to display a random element from the array. Kotlin array reduction Reduction is a terminal operation that aggregates array values into a single value. The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

How to pick up a random string from an array of strings in Java, It is quite easy. You only need to generate a random number that acts as the index value for String array. You can generate random value using Random class� The easiest way to generate a random number in Kotlin is to use java.lang.Math. Below example will generate a random double number between 0 and 1. Below example will generate a random double number between 0 and 1.

Idiomatic way to generate a random alphanumeric string in Kotlin, Idiomatic way to generate a random alphanumeric string in Kotlin. kotlin random Int = 15): String{ val alphanumerics = CharArray(26) { it -> (it + 97).toChar() }. Using get() and set() methods – As you know, an array in Kotlin is basically a class. Therefore, we can access the data of a class object via its member functions. The get() and set() functions are said to be member functions. The get() method takes a single parameter—the index of the element and returns the value of the item at that index

Comments
  • Thank you svoid. This was the fix. Appreciate the explanation.