Get a random sentence from a selection each time in Batch

前端 未结 2 867
感情败类
感情败类 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-28 15:48
    @echo OFF
    SETLOCAL
    SET message0=message zero
    SET message1=message one
    SET message2=message two
    SET message3=message three
    SET message4=message four
    
    :: running 10 times
    
    FOR /l %%i IN (1,1,10) DO CALL :showme
    GOTO :eof
    
    :showme
    SET /a select=%RANDOM% %% 5
    CALL SET message=%%message%select%%%
    ECHO %message%
    GOTO :eof
    
    0 讨论(0)
提交回复
热议问题