Solved Random function in Batch

Hewlett-packard Pavilion g6-2000 15.6" b...
January 25, 2016 at 16:15:06
Specs: Windows 10, 4gb
I'm doing a personal project to help me study for school. It's a trivia game, where it asks a question and you have to type the response correctly to continue. I really wanted it to be a random function though, so I used the command set /a num=%random% %%12 (Because I only have 11 questions so far) and assigned each number to a question. The only problem though is that once the randomized question is correctly answered, it continues down it's normal pattern. (1-11). This causes a problem because not all eleven questions are asked. So I decided to end a correct question with a loop back to the random generator, but then questions were asked multiple times. I would like it to where it will ask all eleven questions randomly, and finish when all eleven have been asked once.

See More: Random function in Batch

Report •

January 25, 2016 at 20:58:14
Lacking the benefit of your current code, I can only give my idea which may not match your approach. That said, I would prob'ly try assigning the questions to numeric variables:
set 1=what's your favorite color
when that question is resolved either way, the variable 1 is then set to null, or undefined. The loop checks if the
variable is defined before asking the question:
if defined %%a call :question "%%a"

Report •

January 27, 2016 at 05:01:13
✔ Best Answer
::====== script starts here ===============
:: ask 11 questions in random order
:: geek.bat 2016-01-27 18:37:23.36
@echo off & setLocal enableDELAYedeXpansioN

for /L %%a in (1 1 11) do set Q%%a=
set N=1

set/a R=!RANDOM!%%11+1
if not defined Q!R! set Q!R!=!N! && set/a N+=1
if !N! gtr 11 goto :done-main
goto :main

for /L %%i in (1 1 11) do echo ask question !Q%%i!
::====== script ends here =================


M2 Get custom script or take private lessons

Report •

Related Solutions

Ask Question