Qbasicnews.com

Full Version: randomizing data inputed from file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can't randomize (i.e. shuffle) the data inputted from the file below.
The data always will display in order from the file. Can I randomize it?

CLS
DEFDBL A-Z
CONST itema = 6
CONST itemb = 7

DIM A(1 TO itema) AS INTEGER
DIM b(1 TO itemb) AS INTEGER

PRINT : PRINT "initial draw:";
FOR A = 1 TO itema
A(A) = A
PRINT A(A);
NEXT A

RANDOMIZE TIMER ' must be outside loop

h = 0
OPEN "data.txt" FOR INPUT AS #1
DO WHILE NOT EOF(1)
h = h + 1
INPUT #1, b(h)
LOOP
CLOSE #1
PRINT



' I want the contents of file displayed below, in random order
' not in same order it is listed in the file.
PRINT : PRINT "random draw: ";
FOR b = 1 TO itemb
PRINT b(b);
NEXT b

'end of program

The file data.txt contains the following 7 datum in this order:

5
2
2
1
3
7
1
You're on the right track except. You need to actually make use of the random statement Wink. What you are doing is inputting the contents of the file in the same order as they are. You need to randomize the variable 'h' so the contents are stored in random locations Wink.

[syntax="qbasic"]RANDOMIZE TIMER
CLS
DIM q(1 TO 10) AS INTEGER

occupied% = 0

DO
h = INT(RND * 10) + 1
IF q(h) = 0 THEN occupied% = occupied% + 1: q(h) = occupied%
LOOP UNTIL occupied% > 9 OR INKEY$ = CHR$(27)

FOR i% = 1 TO 10
PRINT q(i%)
NEXT[/syntax]

The above program generates random numbers from 1 to 10 and stores the data "occupied%" at random locations. Just substitute whatever data you want to store in this statement:

q(h) = occupied%

BTW the limitation of the above method is that you cant store "0" in the array
:wink: