Computing.Net > Forums > Programming > batch extract a random character

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

batch extract a random character

Reply to Message Icon

Name: genet
Date: February 15, 2007 at 11:06:47 Pacific
OS: xp
CPU/Ram: intel 3.6 ghz
Comment:

hey..
i have made a random number gen it creates a random number from 1 to 5 i was wondering if anyone knows a way i could use this to pick a random letter from a string?
set string=abcdefg
somehow i need to get a single character from this string.

here is the number gen.

@echo off
SET C=10
set times=0
:loop
:more
if %times% equ 5 goto :done
set /a times=%times%+1
set /a R=%random:~-1%
if %R% gtr %C% goto :loop
if %R% lss 1 goto :loop
set /a rnd%times%=%R%
if %times% LSS 5 goto :more
:done
echo %rnd1% %rnd2% %rnd3% %rnd4% %rnd5%
pause

thx
genet



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: February 15, 2007 at 11:42:15 Pacific
Reply:

Didn't work for me.

=========================
C:\temp\-\multserv\strlen\rndchar>g
5 9 1

C:\temp\-\multserv\strlen\rndchar>g
3 4 9 1 8

C:\temp\-\multserv\strlen\rndchar>g
3 8 7 2 5

C:\temp\-\multserv\strlen\rndchar>g
9 7 7 3 7

C:\temp\-\multserv\strlen\rndchar>g
1 7 8 7 7

C:\temp\-\multserv\strlen\rndchar>g
6 4 9 6 8


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: dtech10
Date: February 15, 2007 at 15:14:36 Pacific
Reply:

Hi Genet
Just learning to program are we.
Not the way I usually program
but might help with your logic.
The !r! is simlar to %r%
The times=+1 is the same as times=%times%+1
The list= ABCDE The first position is 0 not 1 but as the numbers are 1-5 this position
is never used.
Have fun.

@echo off
setlocal EnableDelayedExpansion
set List= ABCDEF

:again
set times=0
:loop
set r=%random:~-1%
if !r! GTR 5 goto loop
if !r! LSS 1 goto loop
set /a times+=1
set /a rnd!times!=!r!
if !times! LSS 5 goto loop

echo [%rnd1%] [%rnd2%] [%rnd3%] [%rnd4%] [%rnd5%]
echo !List:~%rnd1%,1! !List:~%rnd2%,1! !List:~%rnd3%,1! !List:~%rnd4%,1! !List:~%rnd5%,1!
pause
goto again



0

Response Number 3
Name: Mechanix2Go
Date: February 15, 2007 at 23:55:04 Pacific
Reply:

Hi dtech10,

Yeah, yours works. As you say the positions start at 0. You can eliminate the space and use positions 0 thru 4.

And you can simplify by using a mod 5 instead of range checking.

::==Rchar6.bat
@echo off
setLocal EnableDelayedExpansion
set str=ABCDE
set /a P=!random!%%5
echo !str:~%P%,1!
::====================


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: dtech10
Date: February 16, 2007 at 13:27:22 Pacific
Reply:

Hi Mechanix

Thanks for that.
I used the values 1-5 as that was what he
wanted, much more elegant used modulus.


0
Reply to Message Icon

Related Posts

See More


Problems in compiling D-I... Creating simple makefile ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch extract a random character

Batch Extracting part of a string www.computing.net/answers/programming/batch-extracting-part-of-a-string/15641.html

Batch that grabs a random line? www.computing.net/answers/programming/batch-that-grabs-a-random-line/16888.html

Batch file to count a specific character in a www.computing.net/answers/programming/batch-file-to-count-a-specific-character-in-a/19751.html