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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:10:49+00:00 2026-06-03T15:10:49+00:00

I have a function that will select a random value between a min and

  • 0

I have a function that will select a random value between a min and a max value. So when I have a test where I test that the value fall between the min and max value. But as there where some failures in my app the test passed some times and some times fails due to the randomness.

Would it be a good idea to override/mock Math.random() to return 0 and 1 and test that my value is same as max or min value? Or are there better ways to test randomness in JavaScript?

Here is the function that will be used to create the random number:

function constrain(min, max){
    return Math.round(Math.random() * (max - min) + min)
}
  • 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-06-03T15:10:52+00:00Added an answer on June 3, 2026 at 3:10 pm

    SinonJS is a JavaScript mocking framework which integrates with all of the major JavaScript Unit Testing frameworks. The adapters will take care of ‘reverting’ any stubs / mocks that you create during your tests.

    // Stub out Math.random so it always returns '4' (chosen by fair dice roll)
    sinon.stub(Math, 'random').returns(4);
    
    // Double check that it worked.
    equal(Math.random(), 4, "http://xkcd.com/221/");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that will create a select where clause, but right now
I have a function that will perform a SELECT query from some db and
I have DLL and application that will call some function in this dll. For
Hi I have a function that will generate a random string which works fine.
I have a function that will allow me to edit the manager property of
I have created a function that will return the string path from my DB.
I have a javascript function that will show the image you're about to upload
I have a binary search function that will search for a word in an
I have problem with fancybox. I want to write a function that will run
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead

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.