To test a random return value function.

I want to unit test a function which is returning a random value from an array-list . I want to check whether the function is returning a random value or not .
sriramvemaraju2000Asked:
Who is Participating?
 
reijnemansCommented:
Hi,

TestNG gives some features that may be usefull for testing the random generator:

	private static List<Integer> randomResults = new ArrayList<Integer>(); 
	
	@Test(invocationCount=100, successPercentage=50)
	public void TestRandom() {
		Integer randomResult = random();
		if (randomResults.contains(randomResult)) {
			Assert.fail("result is returned before " + randomResult);
		} 
		System.out.println(randomResult);
		randomResults.add(randomResult);
	}
	
	
	public Integer random () {
		Double r1 = Math.random();
		Double r2 = r1 * 100; 
		return r2.intValue();
	}

Open in new window


pay attention add the @Test
 
0
 
for_yanCommented:
0
 
for_yanCommented:

these are some useful  discussions on this subject and even applet , but no code is provided
http://sprng.fsu.edu/Version2.0/test-suite.html
0
 
sriramvemaraju2000Author Commented:
Thanks for your help.
0
 
for_yanCommented:
You are welcome.
For some reason I cannot find any java source code for testing of random generator;
I'm sure it  should be avaialable

This is code in C:

http://www.fourmilab.ch/random/random.zip

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.