Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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%
pausethx
genet

Didn't work for me.
=========================
C:\temp\-\multserv\strlen\rndchar>g
5 9 1C:\temp\-\multserv\strlen\rndchar>g
3 4 9 1 8C:\temp\-\multserv\strlen\rndchar>g
3 8 7 2 5C:\temp\-\multserv\strlen\rndchar>g
9 7 7 3 7C:\temp\-\multserv\strlen\rndchar>g
1 7 8 7 7C:\temp\-\multserv\strlen\rndchar>g
6 4 9 6 8
=====================================
If at first you don't succeed, you're about average.M2

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 loopecho [%rnd1%] [%rnd2%] [%rnd3%] [%rnd4%] [%rnd5%]
echo !List:~%rnd1%,1! !List:~%rnd2%,1! !List:~%rnd3%,1! !List:~%rnd4%,1! !List:~%rnd5%,1!
pause
goto again

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

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

![]() |
Problems in compiling D-I...
|
Creating simple makefile ...
|

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