0

I have this code -

number1, number2, number3, number4, number5 = Array.new(5) { rand(99999)+1 }

How can I make sure that each number is unique? Also - is it possible to output all numbers as 5 digit? Like 00147 instead of 147?

Thanks for help!

3 Answers3

3
list = []
(list << '%05i' % (rand(99999)+1)).uniq! while list.length < 5
number1, number2, number3, number4, number5 = list
Matt
  • 20,108
  • 1
  • 57
  • 70
  • 1
    You could eliminate `list` with `[].tap {|a| (a << '%05i' % (rand(99999)+1)).uniq! while a.length < 5}`. I prefer the way you've done it, but thought it was worth a mention. – Cary Swoveland Nov 28 '13 at 18:24
  • shouldn't it be while list.length <= 5, as it would not reach 5 elements in the list – bjhaid Nov 28 '13 at 18:24
  • @bjhaid, if you had run the code first you wouldn't be asking. (I say this as someone who recently made an incorrect suggestion in a comment because I hadn't first checked the code. :-) ). – Cary Swoveland Nov 28 '13 at 18:29
  • Can you tell me where is the trick why I cannot use the numbers for mathematical calculations? And what should I change for that? Like, add one for the number or divide by 2 etc. – Reinholds R Razums Nov 28 '13 at 18:33
  • @CarySwoveland gotcha, the list is appended before the clause is reached – bjhaid Nov 28 '13 at 18:38
  • @ReinholdsRRazums it is a string and not an integer – bjhaid Nov 28 '13 at 18:39
  • @ReinholdsRRazums Use `number1.to_i` to use it in calculations. – Matt Nov 28 '13 at 18:46
  • @CarySwoveland I like the `tap` idea, thank you. – Matt Nov 28 '13 at 18:50
  • ...or a slight variant: `{}.tap {|h| h['%05i' % (rand(99999)+1)] = 'cat' while h.length < 5}.keys` (or `require 'set'`, `Set.new`...). – Cary Swoveland Nov 28 '13 at 20:07
0
def get_unique_random(n)
  a = []
  while n > 0 do
    r = "%05d" % (rand(99999)+1)
    (a << r; n -= 1) unless a.include?(r)
  end
  a
end

get_unique_random(5)

[Edit: I fixed an error I had introduced with an edit. (Ever done that?) Previously I had:

r = rand(99999)+1;
(a << "%05d" % r; n -= 1) unless a.include?(r)

I will leave it as an exercise to spot the error.]

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
  • Yeah, I tried with code like this previously... But how can I define the numbers to variables here? I need to insert them in different places later. – Reinholds R Razums Nov 28 '13 at 17:58
  • I don't understand, Reinholds. `get_unique_random(5)` returns an array of five random numbers. – Cary Swoveland Nov 28 '13 at 18:16
  • @Cary Sometimes it will return an array of four random numbers. – steenslag Nov 28 '13 at 21:59
  • @steenslag, I see that, before I made the edit, the array returned may have contained duplicates, but I don't understand how it could have returned four random numbers, considering that `n` is decremented whenever an element is added to the array. Perhaps I'm just a bit dense today. Please explain. – Cary Swoveland Nov 28 '13 at 22:44
  • I seem to be a bit dense today too, missed the that `-=1` is not executed in case of a duplicate. – steenslag Nov 28 '13 at 22:50
0

This seems like the simplest approach to me. Each number is guaranteed to be unique.

array = (1...99999).to_a
unique_randoms = 5.times.map { '%05i' % array.delete_at(rand(array.length)) }
Josh
  • 5,631
  • 1
  • 28
  • 54