You keep the first ball. After that, for the second ball, you keep it with 1/2 probability, for the third ball you keep it with 1/3 probability, and so on. This is easy to due using the [0, 1] random number generator. For example to get 1/3 probability multiply it by 3 and check whether it is less than 1.

Now to show that this works. Let’s say there were N balls (of course you don’t know this in advance). Then the chance you keep the last ball is obviously 1/N. The chance you keep the (N-1)-st ball is 1/(N-1) times (N-1)/N, which is also 1/N. So now you can use an easy induction argument to show that the same is true for every ball.

[/spoiler]