blob: 04063d0ac5c9ee04418ab061b124f884d23977bd [file] [log] [blame]
Simon Hunt3a6eec02015-02-09 21:16:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt3a6eec02015-02-09 21:16:43 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Random -- Encapsulated randomness
19 */
20(function () {
21 'use strict';
22
Simon Hunt3a6eec02015-02-09 21:16:43 -080023 var halfRoot2 = 0.7071;
24
25 // given some value, s, returns an integer between -s/2 and s/2
26 // e.g. s = 100; result in the range [-50..50)
27 function spread(s) {
28 return Math.floor((Math.random() * s) - s / 2);
29 }
30
31 // for a given dimension, d, choose a random value somewhere between
32 // 0 and d where the value is within (d / (2 * sqrt(2))) of d/2.
33 function randDim(d) {
34 return d / 2 + spread(d * halfRoot2);
35 }
36
37 angular.module('onosUtil')
38 .factory('RandomService', ['$log', 'FnService',
39
Steven Burrows1c2a9682017-07-14 16:52:46 +010040 function () {
Simon Hunt3a6eec02015-02-09 21:16:43 -080041 return {
42 spread: spread,
Steven Burrows1c2a9682017-07-14 16:52:46 +010043 randDim: randDim,
Simon Hunt3a6eec02015-02-09 21:16:43 -080044 };
45 }]);
46}());