You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现For循环累加RNG结果,达到指定帧数时自动终止?

解决电影项目随机镜头帧数累加终止的问题

原代码存在的几个问题

  • 循环逻辑错误:你写的for循环是按次数从1到usableRunTime执行,而非累加每次生成的镜头帧数,完全不符合“总和达到或超过可用帧数时终止”的需求
  • 语法错误:console.log语句末尾的+ "\t" + "FRAMES:"超出了方法括号,导致这部分内容无法打印,还会引发语法问题
  • 函数混淆:你提到的输出1-72的ShotRNG,对应代码里的frameRNG函数,而当前shotRNG依赖未定义的totalShotSum变量,运行时会报错
  • 依赖缺失:shotParameterCalculation函数未定义,调用shotVersion时会报错

修正后的实现代码

function partSixDivider(){
    console.log("#006\tPOST PRODUCTION\n[DETERMINES ENTIRE FLOW OF THE FILM BASED UPON THE PREVIOUSLY GENERATED PARAMETERS]\n\n");
}

let usableRunTime = 500; // 直接设置为你需求的500,也可保留转换函数(需确保totalRunTime已定义)

function convertSecondsToFramesDeclaration() {
    // 给totalRunTime设默认值避免报错,实际可根据项目需求传入
    const totalRunTime = typeof totalRunTime !== 'undefined' ? totalRunTime : 36;
    usableRunTime = totalRunTime * 24 - 360;
    return "TOTAL USABLE FRAMES (BASED ON 24FPS):\t" + usableRunTime;
}

// 实现你需求的ShotRNG:生成1-72的随机整数
function shotRNG() {
    return Math.floor(Math.random() * 72) + 1;
}

function frameRNG() {
    return Math.floor(Math.random() * 72) + 1;
}

// 补全缺失的shotParameterCalculation函数
function shotParameterCalculation(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function shotVersion() {
    let versionRNG = shotParameterCalculation(1,4);
    return versionRNG;
}

let introFrames = 240; // 10秒*24帧
let outtroFrames = 120; // 5秒*24帧

function filmDeclaration() {
    console.log("FILM OVERVIEW REPORT\n\n");
    console.log(`INTRO \tVERSION: N/A\tFRAMES: ${introFrames}`);
   
    let totalFramesUsed = 0;
    let shotCount = 1; // 镜头编号从1开始
    
    // 用while循环实现累加终止逻辑
    while (totalFramesUsed < usableRunTime) {
        const currentShotFrames = shotRNG();
        // 可选:最后一次镜头如果超出可用帧数,自动调整为剩余帧数
        const actualFrames = totalFramesUsed + currentShotFrames > usableRunTime 
            ? usableRunTime - totalFramesUsed 
            : currentShotFrames;
        
        console.log(`SHOT ${shotCount}:\tVERSION: ${shotVersion()}\tFRAMES: ${actualFrames}`);
        
        totalFramesUsed += actualFrames;
        shotCount++;
    }

    console.log(`OUTTRO \tVERSION: N/A\tFRAMES: ${outtroFrames}\n\n`);
}

// 调用测试
filmDeclaration();

关键逻辑说明

  1. 用totalFramesUsed记录已消耗的帧数,shotCount记录镜头序号
  2. while循环的核心终止条件是totalFramesUsed >= usableRunTime,每次循环生成随机帧数并累加
  3. 加入了最后一次镜头的帧数修正:如果当前随机帧数加上已用帧数超过限制,自动取剩余帧数(不需要该逻辑的话,直接使用原随机数即可)
  4. 补全了所有缺失的依赖函数,修正了语法错误

内容的提问来源于stack exchange,提问作者Jiyorude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 21:25:24