Created
October 13, 2018 08:35
-
-
Save Binary-Finery/3f7823b5ca8a09db8b25f5d9135d84c8 to your computer and use it in GitHub Desktop.
random uk lotto number generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.RadioButton | |
import kotlinx.android.synthetic.main.activity_main.* | |
import java.util.* | |
/* | |
random UK lottery number generator. | |
generates a specified quantity of random numbers | |
in the range of 1 to 59 (inclusive). | |
quantity of random numbers is selected by the user | |
using radio buttons inside a radio group | |
*/ | |
class MainActivity : AppCompatActivity() { | |
var qty = 6 | |
val max = 59 | |
val list = mutableListOf<Int>() | |
lateinit var str: String | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
supportActionBar?.hide() | |
r5.isChecked = true | |
radio_group.setOnCheckedChangeListener({ group, radio_id -> | |
val radio: RadioButton = findViewById(radio_id) | |
qty = Integer.parseInt(radio.text.toString()) | |
}) | |
button.setOnClickListener { gen() } | |
} | |
fun gen(){ | |
list.clear() | |
str = "" | |
button.isEnabled = false | |
while (list.size < qty) { | |
val n = Random().nextInt(max) + 1 | |
if (!list.contains(n)) list.add(n) | |
} | |
list.sort() | |
for (i in 0 until list.size) | |
str += "${list[i]} " | |
text_view.text = str | |
button.isEnabled = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment