filmov
tv
Get random string element

Показать описание
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Get random string element
I'm trying to create a string of 116 random characters, I'm terrible at understanding batch files but I managed to create a random number rand from 0 to 15 to get an element from hex_chars=0123456789ABCDEF
hex_chars=0123456789ABCDEF
But when I try to get and output an element from a string of characters I constantly encounter problems.
@echo off
setlocal enabledelayedexpansion
REM Define the length of the random hex string
set "hex_length=116"
REM Initialize the hex characters
set "hex_chars=0123456789ABCDEF"
REM Variable to store the random hex string
set "random_hex="
REM Loop until the required length is reached
for /L %%i in (1,1,%hex_length%) do (
REM Generate a random number between 0 and 15
set /a "rand=((%random% * %random% + %random% %% 281 * %%i) + %time:~-2%) %% 16"
echo !rand!
REM Returns 0 every time
echo !hex_chars:~%rand%,1!
REM Returns 0123456789ABCDEFrand every time
echo !hex_chars:~!rand!,1!
REM Get the hex character from hex_chars based on the random number
set random_hex=%random_hex%!hex_chars:~%rand%,1!
)
REM Output the random hex string
echo Random Hex (116 chars): !random_hex!
endlocal
pause
@echo off
setlocal enabledelayedexpansion
REM Define the length of the random hex string
set "hex_length=116"
REM Initialize the hex characters
set "hex_chars=0123456789ABCDEF"
REM Variable to store the random hex string
set "random_hex="
REM Loop until the required length is reached
for /L %%i in (1,1,%hex_length%) do (
REM Generate a random number between 0 and 15
set /a "rand=((%random% * %random% + %random% %% 281 * %%i) + %time:~-2%) %% 16"
echo !rand!
REM Returns 0 every time
echo !hex_chars:~%rand%,1!
REM Returns 0123456789ABCDEFrand every time
echo !hex_chars:~!rand!,1!
REM Get the hex character from hex_chars based on the random number
set random_hex=%random_hex%!hex_chars:~%rand%,1!
)
REM Output the random hex string
echo Random Hex (116 chars): !random_hex!
endlocal
pause
I thought to get the hex_string element via for but I didn't figure out how to do it correctly
I tried changing the % and ! and ^ symbols in !hex_chars:~!rand!,1!
Tags: string,batch-file,randomSource of the question:
Question and source license information: