Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best create a random float in a range between two floats

Tags:

ruby

I know that I can generate random floats with rand(max). I tried to generate a float in a range, this shouldn't be hard. But e.g rand(1.4512) returns 0, thus rand isn't calculating with floats. Now I tried a little trick, converting the thing to an integer and after randomizing a fitting number in my desired range, calculating it back to a float.. which is not working.

My question is how to do this in a better way. If there is no better way, why is this one not working? (Maybe it's too late for me, I should've started sleeping 2 hours ago..). The whole thing aims to be a method for calculating a "position" field for database records so users can order them manually. I've never done something like this before, maybe someone can hint me with a better solution.

Here's the code so far:

def calculate_position(@elements, index)     min = @elements[index].position      if @elements[index + 1].nil?         pos = min + 1     else         pos = min + (rand(@elements[index + 1].position * 10000000000) / 10000000000)     end      return pos end 

Thanks!

like image 577
pdu Avatar asked Nov 10 '09 22:11

pdu


People also ask

How do you generate a random float between ranges in Python?

uniform() to get a random float number within a range. The random. uniform() function returns a random floating-point number between a given range in Python. For example, It can generate a random float number between 10 to 100 Or from 50.50 to 75.5.

Which method is used to generate a random float value?

Random float values can be generated using nextFloat method of the RandomUtils class. nextFloat is a static method that can be used to generate random float values.

How do you generate a random float between two numbers in C++?

Using rand() function Another simple, but less preferred solution to generate pseudo-random numbers in C++ is with rand() function. It returns a random number between 0 and RAND_MAX , and can be used to generate floating-point random values in any arbitrary closed interval.

Can you use floats in range?

The Python range() works only with integers. It doesn't support the float type, i.e., we cannot use floating-point/decimal value in any of its arguments. For example, If you use range() with float step argument, you will get a TypeError 'float' object cannot be interpreted as an integer .


1 Answers

Pass a range of floats to rand

If you want to "create a random float in a range between two floats", just pass a range of floats to rand.

rand(11.2...76.9) 

(Tested with Ruby 2.1)

Edit

According to the documentation: https://ruby-doc.org/core-2.4.0/Random.html

There are two different ways to write the random function: inclusive and exclusive for the last value

rand(5..9)      # => one of [5, 6, 7, 8, 9] rand(5...9)     # => one of [5, 6, 7, 8] rand(5.0..9.0)  # => between 5.0 and 9.0, including 9.0 rand(5.0...9.0) # => between 5.0 and 9.0, excluding 9.0 
like image 136
Nathan Long Avatar answered Oct 07 '22 03:10

Nathan Long