1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestMinimalStandardRandomSequence.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15
16 // .NAME
17 // .SECTION Description
18 // This program tests the vtkMinimalStandardRandomSequence class.
19 //
20 // Correctness test is described in first column, page 1195:
21 // A seed of 1 at step 1 should give a seed of 1043618065 at step 10001.
22 //
23 // ref: "Random Number Generators: Good Ones are Hard to Find,"
24 // by Stephen K. Park and Keith W. Miller in Communications of the ACM,
25 // 31, 10 (Oct. 1988) pp. 1192-1201.
26 // Code is at page 1195, "Integer version 2"
27
28 #include "vtkMinimalStandardRandomSequence.h"
29 #include "vtkDebugLeaks.h"
30 #include "vtkMath.h"
31
TestMinimalStandardRandomSequence(int,char * [])32 int TestMinimalStandardRandomSequence(int,char *[])
33 {
34 vtkMinimalStandardRandomSequence *seq
35 =vtkMinimalStandardRandomSequence::New();
36
37 seq->SetSeedOnly(1);
38
39 bool status=true;
40
41 // Check seed has been set
42 status=seq->GetSeed()==1;
43
44 if(status)
45 {
46 int i=0;
47 while(i<10000)
48 {
49 // cout << "i=" << i << " seed=" << seq->GetSeed()<< endl;
50 seq->Next();
51 ++i;
52 }
53 status=seq->GetSeed()==1043618065;
54 if(!status)
55 {
56 cout << "FAILED: seed is not 1043618065, it is " << seq->GetSeed()
57 << endl;
58 }
59 }
60 else
61 {
62 cout << "FAILED: seed is not 1, it is " << seq->GetSeed() << endl;
63 }
64
65 vtkMath::RandomSeed(1);
66 int i=0;
67 while(i<9997)
68 {
69 // cout << "i=" << i << " seed=" << vtkMath::GetSeed() << endl;
70 vtkMath::Random();
71 ++i;
72 }
73 status=vtkMath::GetSeed()==1043618065;
74 if(!status)
75 {
76 cout << "FAILED: static seed is not 1043618065, it is " << vtkMath::GetSeed()
77 << endl;
78 }
79
80 seq->SetSeed(1);
81 i=0;
82 while(i<9997)
83 {
84 // cout << "i=" << i << " seed=" << vtkMath::GetSeed() << endl;
85 seq->Next();
86 ++i;
87 }
88 status=seq->GetSeed()==1043618065;
89 if(!status)
90 {
91 cout << "FAILED: seed auto is not 1043618065, it is " << seq->GetSeed()
92 << endl;
93 }
94 seq->Delete();
95 int result;
96
97 if(status)
98 {
99 // passed.
100 result=0;
101 }
102 else
103 {
104 // failed.
105 result=1;
106 }
107 return result;
108 }
109