# Random Numbers

Hello my friends.
Im trying to make a random array of 1-10 with 10 elements.
the point is it should not have any same numbers.(it should have 10 diffrent numbers)
here is my code but its not working correctly.it keeps giving me an array with more than once for some numbers.any idea?
fun main() {
var myarray = Array(10) { 0 }
for (i in 0…9) {
var x = Random.nextInt(1, 10)
if (x != myarray[i]) {
myarray[i] = x
}
}
myarray.forEach {
print("\$it ")
}
}

Is this what you need?

`(1..10).shuffled()`

3 Likes

No,i need to use random.nextint and the numbers should be like:
3 8 6 7 9 1 4 2 5 10
10 different random numbers! this is what I need

And how exactly the results of my above code are different than your expectations? Did you even try to run it and see what does it provide?

But if you prefer your solution then you have 3 bugs in your code. First, maximum possible value of `Random.nextInt(1, 10)` is not 10, but 9. Second, this `if (x != myarray[i])` check does not make any sense. You just check if the current element is `x`, but current element is still unset, so it is always `0` - this condition is always true. What you need is to check if `x` exists in the whole array. Third, if your `x` exists already in the array, you just ignore it and step to another element, leaving “empty” space in the array - you should try again with another random number instead. If you fix all these bugs you will get something like this:

``````var myarray = Array(10) { 0 }
for (i in 0..9) {
while (true) {
val x = Random.nextInt(1, 11)
if (x !in myarray) {
myarray[i] = x
break
}
}
}
``````

I guess this code provides the results you need, but… still, it does exactly the same thing as my one-liner above - just in much more obscure and less performant way.

1 Like

Hi again
I actually tried the " .shuffled() " code and it worked perfectly.the reason why I said it has to be with "
Random. " was because im trying to understand its algorithm.the second code u sent I just pasted it but its not giving me any answer,now im gonna try to work on it.and BTW you were right,the condition doesnt even make sence…im new and im trying to figure things out in terms of understanding the machine language…I will let you know about the answer for this and really appreciate for the help!I do respect your time and knowledg

1 Like

Ah, ok, if you’re learning then it makes sense to do this with whatever seems the most straightforward to you. I have two suggestions for you to consider.

As you only add at the end of `myarray`, it would be probably easier to use mutable list instead of array and just add to it. You would not need to initialize it with zeroes, put elements at the right spot and most importantly: searching through array for existing items (`x !in myarray`) always checks all 10 elements, even if we only added 2 so far. List would check only these 2. However, I understand sometimes we just prefer arrays and your task can be accomplished with them.

Also, this algorithm has big performance overhead. It is not that important if there are only 10 items, but if you would like to create 1000 items then it would be significant. The problem is that when we already have 999 items and we need to find the last one, it is hard to get it by random. Each new random try requires us to iterate over 999 existing items to check whether it is unique, so If we succeed at 500th try, we have to perform 500,000 checks. Just to find the last item. You can optimize this by creating a set/list of unused items, then pick from it randomly, removing at the same time.

I won’t provide you with a working example to not spoil the fun

Hi dear broot
I have tried new algorithm that kind of does the same as yours.here is it:
var i = 0
while (i <= 9) {
var n = Random.nextInt(1, 11)
var find = 0
for (j in 0…9) {
if (myarray[j] == n) {
find = 1
break
}
}
if (find == 0) {
myarray[i] = n
++i
}
}
myarray.forEach {
print("\$it ")
}
I know it has more code lines compare to yours,but I would appreciate if you let me know about the CONS on this one.
*****BTW I have just began learning *Collections which the im on the very first topic that is “listOf”
so im working on it and will try to write the code using your advice(operiting random numbers in a List)
thank you so much again man,I really appreciate the way you care and dedicate your valuable time for helping me.

Hi dear
I also have a general question.im not sure where you are living,but im in Iran and my wife and I we are about to move to U.S.
the only reason why I have chosen KOTLIN is im trying to learn something to search for a job in L.A.
you as an experienced programmer I want to know your opinion about the path I have chosen and please let me know if I study hard,how long do I need to be able to do some online jobs and where can I find online small project to be paid just for the begining? @broot

Well, this code seems to be correct, I don’t see any bugs Now it is time for optimization

1. Note that you don’t need to search the whole array for existing items. For example, if `i = 5` then array contains only elements `0..4`, but your `j` loop always checks all `0..9` items.
2. 8 lines of code between `var find = 0` and `if (find == 0) {` are there basically to check if an item exists in the array. This is really a very basic operation regarding collections and usually there is no need to do it manually, even for learning purposes. You can replace this whole code with simple: `if (n !in myarray) {` (but then you won’t be able to use optimization mentioned above).
3. The performance problem I mentioned earlier. Modify your code, so it takes the array size as the parameter and count how many `Random.nextInt()` you need for different sizes. You will see it is much bigger than number of items and it increases faster than linearly. This is because we need to guess remaining items randomly. Even if there is only one remaining item, we still need to find it by random. Try to create a collection of remaining items and remove from it by random - it should be much more efficient, especially for big number of items.
1 Like

I wouldn’t bet on Kotlin as a first job, no prior experience. As much as I hate to say it, something for web developement like JavaScript might make it easier to land a first job.

Also Python might be easier to learn as a first language.

1 Like