Get a random sentence from a selection each time in Batch

前端 未结 2 868
感情败类
感情败类 2021-01-28 14:50

Is there a way of making it so instead of saying the same echo that you set every time, you can give a list of echos and it chooses a random one to say each time it reaches that

2条回答
  •  梦毁少年i
    2021-01-28 15:43

    Yep. Here's a proof of concept.

    @echo off
    setlocal enabledelayedexpansion
    
    set string[0]=This is the first random line.
    set string[1]=This is the second random line.
    set string[2]=This is the third random line.
    
    set /a idx=%random% * 3 / 32768
    
    echo !string[%idx%]!
    

    Here's more info on generating random numbers in Windows batch scripting.

提交回复
热议问题