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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:27:05+00:00 2026-05-18T05:27:05+00:00

I am working on a test project to test a neural networks library… The

  • 0
  • I am working on a test project to test a neural networks library…
  • The problem is that this library sometimes uses random numbers..
  • I need to derive test cases (input,expected output,actual output)…

Does anybody have an idea how to derive test cases (input,expected output,actual output) to a function that uses random numbers when taking actions and evaluating outputs??

  • 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-18T05:27:05+00:00Added an answer on May 18, 2026 at 5:27 am

    Yes, you either have to run a large enough number of cases so that the randomness averages out, or you make the random source another input to your function or method so you can test it independently.

    An example of the first kind (this is Python, but the principle can apply in any language).

    def test_random_number():
        total = sum(random.uniform(0, 1) for _ in xrange(1000))
        assert 100 < total < 900
    

    So this test can fail if you’re unlucky, but it’s still a reasonable test since it’ll pass nearly all the time, and it’s pretty simple to make this kind of test.

    To do things ‘properly’, you need to inject the random source.

    class DefaultRandomBehavior(object):
        def pick_left_or_right(self):
            return random.choice(['left', 'right'])
    
    class AardvarkModeller(object):
        def __init__(self, random_source=None):
            self.random_source = random_source or DefaultRandomBehavior()
    
        def aardvark_direction(self):
            r = self.random_source.pick_left_or_right()
            return 'The aardvark faces ' + r
    

    Now you can unit test this by either mocking out or faking the DefaultRandomBehavior class, thus completely side-stepping the non-determinism.

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

Sidebar

Related Questions

I'm working on a utility that will be used to test the project I'm
I did the following: Create a working Android project (not a library or test
I am working on a new test project with watiN, but always get this
We have multiple developers working on the same unit test project that have unique
Background: Trevor was working on a test project solely for the purpose of trying
I'm working on a project which I'm really not sure how to unit test.
I'm currently working on an Silverlight 3 project, I'm using 2 machines to test
Working with Test::Unit and Shoulda. Trying to test Users.create . My understanding is that
I have mocks working where I test that methods on my mocked object are
I'm working with a test project based on WhoCanHelpMe, which is based on Sharp

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.