// 生成一个连续的整数数组(开始值,长度)
private createSuccessiveIntArray(beginValue: number, length: number): number[]
{
let array = new Array<number>();
for (let i = 0; i < length; i++)
{
array.push(beginValue + i);
}
return array;
}
// 打乱一个数组
private shuffleArray<T>(currentArray: T[]): T[]
{
// 从数组的最后一个序号开始,依次往前设置各序号对应的值
let maxIndex:number = currentArray.length - 1;
while (maxIndex > 0)
{
// 随机选得一个序号(已设置过值的序号不再参与抽取)
const curIndex = randomRangeInt(0, maxIndex);
// 当前选得的随机值
const temp:T = currentArray[curIndex];
// 将要设置值的序号的原值暂存起来
currentArray[curIndex] = currentArray[maxIndex];
// 设置当前目标序号的值为随机选得的值
currentArray[maxIndex] = temp;
maxIndex--;
}
return currentArray;
}
// 将原始数组生成指定长度的新数组
private adjustArrayLength<T>(originalArray: T[], targetLength: number, shouldShuffle :boolean = true): T[]
{
const singleLength = originalArray.length;
const repeatCnt = Math.floor(targetLength / singleLength);
const remainCnt = targetLength % singleLength;
let retArray = new Array<T>();
for (let i = 0; i < repeatCnt; i++)
{
let curArray = originalArray.slice();
if (shouldShuffle)
{
curArray = this.shuffleArray(curArray);
}
retArray = retArray.concat(curArray);
}
let curArray = originalArray.slice();
if (shouldShuffle) {
curArray = this.shuffleArray(curArray);
}
for (let i = 0; i < remainCnt; i++)
{
retArray.push(curArray[i]);
}
return retArray;
}
网友评论