Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014,2015 Open Networking Laboratory |
| 3 | * |
| 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 | |
| 23 | var $log, fs; |
| 24 | |
| 25 | var halfRoot2 = 0.7071; |
| 26 | |
| 27 | // given some value, s, returns an integer between -s/2 and s/2 |
| 28 | // e.g. s = 100; result in the range [-50..50) |
| 29 | function spread(s) { |
| 30 | return Math.floor((Math.random() * s) - s / 2); |
| 31 | } |
| 32 | |
| 33 | // for a given dimension, d, choose a random value somewhere between |
| 34 | // 0 and d where the value is within (d / (2 * sqrt(2))) of d/2. |
| 35 | function randDim(d) { |
| 36 | return d / 2 + spread(d * halfRoot2); |
| 37 | } |
| 38 | |
| 39 | angular.module('onosUtil') |
| 40 | .factory('RandomService', ['$log', 'FnService', |
| 41 | |
| 42 | function (_$log_, _fs_) { |
| 43 | $log = _$log_; |
| 44 | fs = _fs_; |
| 45 | |
| 46 | return { |
| 47 | spread: spread, |
| 48 | randDim: randDim |
| 49 | }; |
| 50 | }]); |
| 51 | }()); |