Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1081 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get a random number in Ruby

#11
Simplest answer to the question:

rand(0..n)
Reply

#12
you can do rand(range)

x = rand(1..5)
Reply

#13
Maybe it help you. I use this in my app

[To see links please register here]

class String

# Create a random String of given length, using given character set
#
# Character set is an Array which can contain Ranges, Arrays, Characters
#
# Examples
#
# String.random
# => "D9DxFIaqR3dr8Ct1AfmFxHxqGsmA4Oz3"
#
# String.random(10)
# => "t8BIna341S"
#
# String.random(10, ['a'..'z'])
# => "nstpvixfri"
#
# String.random(10, ['0'..'9'] )
# => "0982541042"
#
# String.random(10, ['0'..'9','A'..'F'] )
# => "3EBF48AD3D"
#
# BASE64_CHAR_SET = ["A".."Z", "a".."z", "0".."9", '_', '-']
# String.random(10, BASE64_CHAR_SET)
# => "xM_1t3qcNn"
#
# SPECIAL_CHARS = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "|", "/", "?", ".", ",", ";", ":", "~", "`", "[", "]", "{", "}", "<", ">"]
# BASE91_CHAR_SET = ["A".."Z", "a".."z", "0".."9", SPECIAL_CHARS]
# String.random(10, BASE91_CHAR_SET)
# => "S(Z]z,J{v;"
#
# CREDIT: Tilo Sloboda
#
# SEE:
#
# TODO: Move to random.rb in standard library?

def self.random(len=32, character_set = ["A".."Z", "a".."z", "0".."9"])
chars = character_set.map{|x| x.is_a?(Range) ? x.to_a : x }.flatten
Array.new(len){ chars.sample }.join
end

end

[To see links please register here]


It works fine for me
Reply

#14
How about this one?

num = Random.new
num.rand(1..n)
Reply

#15
range = 10..50

**rand(range)**

or

**range.to_a.sample**

or

**range.to_a.shuffle**(this will shuffle whole array and you can pick a random number by first or last or any from this array to pick random one)
Reply

#16
You can simply use `random_number`.

If a positive integer is given as n, `random_number` returns an integer: 0 <= `random_number` < n.

Use it like this:

any_number = SecureRandom.random_number(100)

The output will be any number between 0 and 100.
Reply

#17
Easy way to get random number in ruby is,

def random
(1..10).to_a.sample.to_s
end
Reply

#18
Use [`rand(range)`][1]

From [Ruby Random Numbers][2]:

> If you needed a random integer to simulate a roll of a six-sided die, you'd use: `1 + rand(6)`. A roll in craps could be simulated with `2 + rand(6) + rand(6)`.
>
> Finally, if you just need a random float, just call `rand` with no arguments.

----------

As [Marc-André Lafortune][3] mentions in [his answer below (go upvote it)][4], [Ruby 1.9.2 has its own `Random` class][5] (that Marc-André himself [helped to debug][6], hence the [1.9.2 target][7] for that feature).

For instance, in this [game where you need to guess 10 numbers][8], you can initialize them with:

10.times.map{ 20 + Random.rand(11) }
#=> [26, 26, 22, 20, 30, 26, 23, 23, 25, 22]

Note:

- Using `Random.new.rand(20..30)` (using `Random.new`) generally would not be a good idea, as explained in detail (again) by [Marc-André Lafortune][9], in [his answer][10] (again).

- But if you don't use `Random.new`, then the [class method `rand`][11] only takes a `max` value, not a `Range`, as [banister][12] (energetically) points out in the comment (and as documented in the [docs for `Random`][13]). Only the [instance method][14] can take a `Range`, as illustrated by

[To see links please register here]

.

This is why the equivalent of `Random.new.rand(20..30)` would be `20 + Random.rand(11)`, since `Random.rand(int)` returns “a random integer greater than or equal to zero and *less than the argument*.” `20..30` includes 30, I need to come up with a random number between 0 and 11, excluding 11.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

[7]:

[To see links please register here]

[8]:

[To see links please register here]

[9]:

[To see links please register here]

[10]:

[To see links please register here]

[11]:

[To see links please register here]

[12]:

[To see links please register here]

[13]:

[To see links please register here]

[14]:

[To see links please register here]

Reply

#19
You can use ruby [rand][1] method for this like below:

rand(n+1)

You need to use `n+1` as the rand method returns any random number greater than or equal to 0 but less than the passed parameter value.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through