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 6097343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:01:57+00:00 2026-05-23T13:01:57+00:00

So I use such script for random int generation inside of range function randomInRange(start,

  • 0

So I use such script for random int generation inside of range

function randomInRange(start, end)
{
  if ((start >= 0) && (end >= 0))
  {
    return Math.round(Math.abs(start) + (Math.random() * (Math.abs(end) - Math.abs(start))));

  }
  else if ((start <= 0) && (end <= 0))
  {
    return 0 - (Math.round(Math.abs(start) + (Math.random() * (Math.abs(end) - Math.abs(start)))));
  }
  else
  {
    return Math.round(((start) + Math.random() * (end - start)));
  }

}

You can see it at work here. for Positive ranges its correct, for negative its correct but I get bad and wrong results for mixed. Why and how to fix it?

I try to use formula like Math.round(start + Math.random() * (end - start));

  • 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-23T13:01:57+00:00Added an answer on May 23, 2026 at 1:01 pm

    OK, I found the problem, you’re performing algebra on strings (since in your code $('fnt').value is the value of an input box, which is a string), not numbers, so things like + will end up concatenating strings and not adding their numeric content. In your particular example, you have:

    Math.round(((start) + Math.random() * (end - start)))
    

    Which evaluates to:

    Math.round((('13') + Math.random() * ('-666' - '13'))) 
    

    Which evaluates to (for example):

    Math.round("13-339.44615370430984")
    

    Since '13' + '-339.44615370430984' will be concatenated, and finally the Math.round call will return NaN

    You should have:

    function randomInRange(start, end) {
      start = Number(start); end = Number(end);
      return Math.round(start + Math.random() * (end - start));
    }
    

    Or change the values you pass into the function, making sure they’re numbers.

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

Sidebar

Related Questions

I have a function in C++ such as: void* getField(interface* p) { int* temp
When I use the Response.Write() method in asp.net to output javascript code, such as
Please Note: answers along the lines of 'use jquery' or 'use (insert wellknown framework)'
I found the source of the problem #2. It is the use of session_register(foo)
Is there an IDE/Tool/script/something that can show call hierarchy and/or data flow in Scala+Java
I am creating a script which may be used on a variety of websites.
I'm trying to use KnockoutJS to build an interactive dashboard with data bindings on
I have an error trying to use $(document).ready() as in the above image. What
I'm trying to execute a .bat script in a C# windows service but it
let me start by saying it may look simple but im finding it extremely

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.