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

Have such script: #! /bin/bash typeset -i i END let END=500 i=1 remainder=1 accum=use
When I use an import such as <script type=text/javascript src=http://o.aolcdn.com/dojo/1.2.3/dojo/dojo.xd.js djConfig=parseOnLoad:true, isDebug: true></script> I
To use such great function as ConvertAll() , I have to convert IList to
I use such script to open pop-ups : <script> var wins = []; openWin
I have such script: (Script.pl) #!/usr/bin/perl use strict; use warnings; use encoding 'utf-8'; use
i use such code, but it renders with error <li class=dd0><div id=dt1<a href=http://localhost:1675/Category/29-books.aspx>Books</a></div></li> there
Is it good to use such approach for keeping read-only list of string, for
How to use such functionality of windows-7 with WPF (one that uses Itunes -
Is it safe to use such code? Response.Cookies[cookieName].Path = Request.ApplicationPath + /; I want
I normally in my projects use such these code: If user.IsInRole(Admin) Then deleteButton.Visible =

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.