Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C039 - Fixing issues of Math.random() #9967

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion web/client/components/TOC/fragments/legend/Legend.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {
addAuthenticationToSLD,
clearNilValuesForParams
} from '../../../../utils/SecurityUtils';
import {
randomInt
} from '../../../../utils/cesium/MathUtils';
import Message from '../../../I18N/Message';

class Legend extends React.Component {
Expand Down Expand Up @@ -54,7 +57,7 @@ class Legend extends React.Component {
getUrl = (props, urlIdx) => {
if (props.layer && props.layer.type === "wms" && props.layer.url) {
const layer = props.layer;
const idx = !isNil(urlIdx) ? urlIdx : isArray(layer.url) && Math.floor(Math.random() * layer.url.length);
const idx = !isNil(urlIdx) ? urlIdx : isArray(layer.url) && Math.floor(randomInt(layer.url.length));

const url = isArray(layer.url) ?
layer.url[idx] :
Expand Down
4 changes: 2 additions & 2 deletions web/client/components/map/cesium/plugins/WMSLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { optionsToVendorParams } from '../../../../utils/VendorParamsUtils';
import { addAuthenticationToSLD, getAuthenticationHeaders } from '../../../../utils/SecurityUtils';

import { isVectorFormat } from '../../../../utils/VectorTileUtils';

import {randomInt} from "../../../../utils/cesium/MathUtils";
function getQueryString(parameters) {
return Object.keys(parameters).map((key) => key + '=' + encodeURIComponent(parameters[key])).join('&');
}
Expand All @@ -38,7 +38,7 @@ function wmsToCesiumOptionsSingleTile(options) {
srs: "EPSG:4326"
}, params || {}, getAuthenticationParam(options));

const url = (isArray(options.url) ? options.url[Math.round(Math.random() * (options.url.length - 1))] : options.url) + '?service=WMS&version=1.1.0&request=GetMap&'
const url = (isArray(options.url) ? options.url[Math.round(randomInt(options.url.length - 1))] : options.url) + '?service=WMS&version=1.1.0&request=GetMap&'
+ getQueryString(addAuthenticationToSLD(parameters, options));
const headers = getAuthenticationHeaders(url, options.securityToken);
return {
Expand Down
3 changes: 2 additions & 1 deletion web/client/components/maps/forms/Thumbnail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import Message from '../../../components/I18N/Message';
import { getResourceIdFromURL } from '../../../utils/ResourceUtils';
import { randomInt } from '../../../utils/cesium/MathUtils';
import Thumbnail from '../../misc/Thumbnail';

const errorMessages = {
Expand Down Expand Up @@ -153,7 +154,7 @@ class MapThumbnail extends React.Component {
d += performance.now(); // use high-precision timer if available
}
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (d + Math.random() * 16) % 16 | 0;
const r = (d + randomInt(16)) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
});
Expand Down
4 changes: 2 additions & 2 deletions web/client/components/style/vector/marker/SymbolLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import axios from 'axios';
import Slider from '../../../misc/Slider';
import Message from '../../../I18N/Message';
import { DEFAULT_SHAPE, DEFAULT_PATH, checkSymbolsError } from '../../../../utils/AnnotationsUtils';

import { randomInt} from '../../../../utils/cesium/MathUtils';
/**
* Styler for the layout of the symbol
*/
Expand Down Expand Up @@ -157,7 +157,7 @@ class SymbolLayout extends React.Component {
symbols.map(s => ({
label: s.label || s.name,
value: s.name,
symbolUrl: this.props.symbolsPath + s.name + ".svg?_t=" + Math.random()
symbolUrl: this.props.symbolsPath + s.name + ".svg?_t=" + randomInt()
})), true
);
} else {
Expand Down
10 changes: 6 additions & 4 deletions web/client/utils/ColorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
import tinycolor from 'tinycolor2';
import { toNumber, includes } from 'lodash';
import { randomInt } from './cesium/MathUtils';

let ColorUtils;
/**
* Porting of various MapStore(1) utilities for random/color scale generations
Expand Down Expand Up @@ -195,12 +197,12 @@ export const colorToRgbaStr = (color, alpha, defaultColor) => {
*/
export const generateRandomHexColor = (currentColors) => {
const roundValue = Math.round;
const randomValue = Math.random;
const randomValue = randomInt;
var maxValue = 255;
const rgbRandomColor = [
roundValue(randomValue() * maxValue),
roundValue(randomValue() * maxValue),
roundValue(randomValue() * maxValue)
roundValue(randomValue(maxValue)),
roundValue(randomValue(maxValue)),
roundValue(randomValue(maxValue))
];
const hexRandomColor = ColorUtils.rgbToHex(rgbRandomColor);
if (currentColors) {
Expand Down
18 changes: 18 additions & 0 deletions web/client/utils/cesium/MathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ export function computeSlopes(coordinates, cameraPosition) {
return null;
}).filter(slope => slope !== null);
}

/**
* Return values in the range of [0, 1)
*/
export const randomFloat = function() {
const int = window.crypto.getRandomValues(new Uint32Array(1))[0];
return int / 2 ** 32;
};

/**
* Return integers in the range of [min, max)
*
* @todo check that min is <= max.
*/
export const randomInt = function(max, min = 0) {
const range = max - min;
return Math.floor(randomFloat() * range + min);
};
Loading