Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 459719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:48:03+00:00 2026-05-12T22:48:03+00:00

I know that I can generate random floats with rand(max). I tried to generate

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:48:04+00:00Added an answer on May 12, 2026 at 10:48 pm

    Let’s recap:

    rand() will generate a (psuedo-)random
    float between 0 and 1.

    rand(int) will generate a
    (psuedo-)random integer between 0 and
    int.

    So something like:

    def range (min, max)
        rand * (max-min) + min
    end
    

    Should do nicely.

    Update:

    Just tested with a little unit test:

    def testRange
        min = 1
        max = 100
    
        1_000_000.times { 
            result = range min, max
            print "ERROR" if result < min || result  > max
        }
    end
    

    Looks fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the Random class can generate pseudo-random numbers but is there a
I know that it's a subject that can raise a lot of debate, but
I know that this can be easily done by using if(i%5 == 0 OR
I know that I can use cmp, diff, etc to compare two files, but
I know that I can iterate over an object's properties like this: for (property
Does anyone know the name of a codec that can translate any random assortment
Does anyone know of a tool that can inspect a specified schema and generate
I need to generate random UNIQUE numbers within a range, how can I do
Hey guys, I know that there are a million questions on random numbers, but
Using PHP, what are some ways to generate a random confirmation code that can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.