Qbasicnews.com
December 15, 2019, 06:35:02 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: randomizing data inputed from file  (Read 1233 times)
janed
New Member

Posts: 5


« on: October 31, 2004, 12:18:07 PM »

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
Logged
TheBigBasicQ
*/-\*
*****
Posts: 4550



WWW
« Reply #1 on: November 01, 2004, 10:52:56 AM »

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:
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!