random()
random()
API 将提供在 0
和 1
之间的确定性伪随机值。与 Math.random()
函数不同,Remotion 函数接受一个种子,该种子可以是一个 number
或一个 string
。如果种子相同,则输出始终相同。
ts
import {random } from "remotion";constrand =random (1); // 0.07301638228818774constrand2 =random (1); // still 0.07301638228818774constrandomCoordinates = newArray (10).fill (true).map ((a ,i ) => {return {x :random (`random-x-${i }`),y :random (`random-y-${i }`),};}); // will always be [{x: 0.2887063352391124, y: 0.18660089606419206}, ...]// @ts-expect-errorrandom (); // Error: random() argument must be a number or a string
ts
import {random } from "remotion";constrand =random (1); // 0.07301638228818774constrand2 =random (1); // still 0.07301638228818774constrandomCoordinates = newArray (10).fill (true).map ((a ,i ) => {return {x :random (`random-x-${i }`),y :random (`random-y-${i }`),};}); // will always be [{x: 0.2887063352391124, y: 0.18660089606419206}, ...]// @ts-expect-errorrandom (); // Error: random() argument must be a number or a string
使用情况
随机性可用于创建有趣的可视化效果,例如粒子效果。由于 Remotion 在多个线程上渲染视频并多次打开网站,因此通过 Math.random()
调用返回的值在多个线程中不同,这使得基于随机性创建动画变得困难。使用此 API 将确保伪随机数始终相同。
访问真随机性
调用 Math.random()
在 Remotion 中会导致 ESLint 警告,因为通常会导致渲染中的错误。如果您确定要一个真随机数,并且想要绕过此消息而不添加忽略注释,请使用 random(null)
ts
// Passing null will result in a different value every time.random (null) ===random (null); // false
ts
// Passing null will result in a different value every time.random (null) ===random (null); // false