Computing.Net > Forums > Programming > Resetting seed w/ out RUN

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Resetting seed w/ out RUN

Reply to Message Icon

Name: Mr. Reyth
Date: May 20, 2005 at 03:27:58 Pacific
OS: Win98
CPU/Ram: N/A
Comment:

Hi I'm using QuickBasic 7 and I am looking to do the opposite of what most people want conerning random numbers. I WANT the predetermined list of numbers and I want to have access to them at will.

I want to be able to set the seed through the randomize statement with a particular number and get the beginning of that same list associated with that number.

The problem is I want to do it a virtually unlimited number of times during a single run of my program.

In my experiments with the randomize function I have found that it does not reset the seed to the beginning of the list as it would if the seed number was used in a fresh RUN.

That's my quandry. How do I get that fresh RUN quality without actually doing a fresh RUN?

As a possible end-round run I am thinking of generating a list of 1000, or 10000 or maybe even 100000 random numbers strung together in a data statement or in a file and then indexing every digit. When I want access to a particular same string of digits I just use the index information and voila I get the same list every time. The two numbers I think I need are the start location in the list and the interval value for number picks. Like one particular seed would start with the 357th number and pick numbers at an interval of 9 so the 2nd number would be the 366th in the list. This should generate the "random" feel huh?

Thanks for your time.

Mr. Reyth



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: May 20, 2005 at 09:17:45 Pacific
Reply:

I have tried the following code in both FreeBASIC and PowerBasic and they both repeat the same random sequence. I don't have QuickBasic 7.

again:
randomize 32768
for x = 1 to 10
print rnd(1)
next x
print "cr to repeat esc to quit "
another:
z$ = inkey$
if z$ = "" then goto another
if asc(z$) = 27 then print "end" : end
goto again


0

Response Number 2
Name: Mr. Reyth
Date: May 20, 2005 at 12:18:43 Pacific
Reply:

Thanks. Here is the very straight-forward code that tests some simple data manipulation and the use of the randomize statement. When performing the following sequence it fails:

1) Hit new universe
2) Write down the universe code
3) Continue through to the summary display and write down the number of planets and wormholes.
4) Hit e(X)it and then re-run.
5) Enter existing universe.
6) Here I get a different number of planets and wormholes (a different sequence).
7) When I then exit and re-run hitting existing universe, initially I get the same numbers as the second report. But they then change with each subsequent (r)e-run and (e)xisting universe I do.
8) When I replace line 235 with RUN instead of goto 20 I at least get the same sequence as the second report.

The only way I can get a sequence to repeat is to re-RUN the program.

I have tested this theory independently with even more simple code with the same results.

It seems like a quirk with Q7 and if there is no way around the problem I will use my own manual seeding as described above.

If you actually take the time with this, my sincere thanks.

Reyth

10 ' *** Galaxy Creator ***
15 RANDOMIZE -32768: DIM pl(25), wh(8)
20 CLS : INPUT "(N)ew universe or (E)xisting universe"; a$
30 IF a$ = "e" THEN 560
40 IF a$ = "n" THEN 60
50 GOTO 20
60 ' Initializing NEW universe
65 RANDOMIZE TIMER: CLS
70 UI = INT((32767 - 1 + 1) * RND + 1)
75 IF INT((2 - 1 + 1) * RND + 1) = 2 THEN UI = UI * -1
80 PRINT : PRINT "Universe ID# is: "; UI
90 PRINT : PRINT "Write this down in order to access this universe again."
100 PRINT : PRINT "Hit <c> to continue or <r> to regenerate."
105 a$ = INKEY$: IF a$ = "" THEN 105
110 IF a$ = "c" THEN 130
120 IF a$ = "r" THEN 65
125 GOTO 105
130 ' Concrete galaxy generation formula
140 RANDOMIZE UI
150 ' number of planets
160 pls = INT((25 - 5 + 1) * RND + 1)
170 'number of wormholes
180 who = INT((8 - 1 + 1) * RND + 1)
185 GOSUB 290: GOSUB 340 'create unique data
190 CLS : PRINT "Number of planets"; pls
200 PRINT : PRINT "Number of wormholes"; who
210 PRINT : PRINT "(w) (p) (x) (r)"
220 a$ = INKEY$: IF a$ = "" THEN 220
230 IF a$ = "x" THEN END
235 IF a$ = "r" THEN 20
240 IF a$ = "w" THEN 260
245 IF a$ = "p" THEN 430
250 GOTO 220
260 CLS : INPUT "Enter the wormhole number to exit this galaxy"; wn
270 IF wn > who OR wn < 1 THEN 260
280 GOSUB 380
285 GOTO 190
290 'planetary color generator
300 RANDOMIZE UI
310 FOR i = 1 TO pls: pl(i) = INT((3 - 1 + 1) * RND + 1): NEXT i
320 RETURN
330 ' wh seed modifier code generator
340 RANDOMIZE UI
350 FOR i = 1 TO who: wh(i) = INT((32767 - 1 + 1) * RND + 1)
360 IF INT((2 - 1 + 1) * RND + 1) = 2 THEN wh(i) = wh(i) * -1
370 NEXT i: RETURN
380 ' apply wh code
390 CLS : PRINT "Wormhole seed code is"; wh(wn)
400 PRINT : PRINT "Hit any key to continue"
410 a$ = INKEY$: IF a$ = "" THEN 410
420 RETURN
430 ' Planetary color display
440 CLS : INPUT "Which planet do you wish to visit"; pc
450 IF pc < 5 OR pc > pls THEN 440
460 CLS : IF pl(pc) = 1 THEN 500
470 IF pl(pc) = 2 THEN 510
480 IF pl(pc) = 3 THEN 520
490 PRINT "data error planet color not received": END
500 PRINT "This planet is a rich red color.": GOSUB 530: GOTO 190
510 PRINT "This planet is a deep blue color.": GOSUB 530: GOTO 190
520 PRINT "This planet is a vibrant green color.": GOSUB 530: GOTO 190
530 PRINT : PRINT "Press any key to continue"
540 a$ = INKEY$: IF a$ = "" THEN 540
550 RETURN
560 ' Generate existing universe
570 CLS : INPUT "Enter the Universe ID#"; UI
580 IF UI < -32767 OR UI > 32768 THEN 570
590 GOTO 140


0

Response Number 3
Name: Mr. Reyth
Date: May 20, 2005 at 12:39:37 Pacific
Reply:

Here is a much simpler example. With the randomize statement at the beginning of the loop the numbers rolled should never change. When I run this program they do.

'RANDOMIZE TIMER
DO
'Simulate rolling two dice using RND.
RANDOMIZE -32768
D1 = INT(RND * 6) + 1
D2 = INT(RND * 6) + 1
'Report the roll.
CLS 'Clear screen.
PRINT "You rolled a"; D1; "and a"; D2; "for a total of"; D1 + D2
INPUT "Roll again (Y/N)"; Resp$
PRINT
LOOP UNTIL UCASE$(MID$(Resp$, 1, 1)) = "N"
END


I think this proves the Q7 quirk and if there are no ways to force the quirk to work I will make my own seed.

Thanks,

Reyth


0

Response Number 4
Name: wizard-fred
Date: May 21, 2005 at 02:54:29 Pacific
Reply:

Have your considered switching BASICS? FreeBASIC is a 32bit compiler version of QBASIC with Windows and Command Prompt Versions (plus Linux). Free and takes old code with minimum conversion. www.freebasic.net


0

Response Number 5
Name: Mr. Reyth
Date: June 4, 2005 at 01:24:27 Pacific
Reply:

Wow thanks. I will check that out.

In case anyone has the same problem and wants a random number generator I will post the code below.

I'm not saying it's bug free but under initial testing it works.

*** PLEASE NOTE: Due to some last minute editing and technical dificulties this will be the first version. I believe there will be some problems with the zero handler that I will need to work out and I will post the fix once I have done that. It works nonetheless. ***

It is based on a file called "seed" that has a list of 100000 single digits from 0-9. That file can be generated with the following program:

5 INPUT "read or write or test"; a$: IF a$ = "t" THEN 60
7 IF a$ = "w" THEN 90
10 OPEN "seed" FOR RANDOM AS 1
20 GET #1, INT((9000 - 1 + 1) * RND + 1), sd
30 PRINT sd
40 INPUT a$: IF a$ = "x" THEN END
50 GOTO 20
60 PRINT INT((9 - 0 + 1) * RND + 0)
70 INPUT a$: IF a$ = "x" THEN END
80 GOTO 60
90 OPEN "seed" FOR RANDOM AS 1
100 FOR I = 1 TO 100000: rd = INT((9 - 0 + 1) * RND + 0)
110 PUT #1, , rd: PRINT rd: NEXT I: GOTO 5

Initially you can run this and first test some random numbers to make sure the file will have digits from 0-9. Then you can write the file. Then you can read the file to make sure the output is correct. This will also allow you to make sure that the I/O commands are properly formatted to your BASIC version (just in case, who knows?).

Then, once the file is generated, the following code will pull a random sequence of numbers from the file. There are several variables of importance:

1) "SC": This is the unique seed identifier to insure you get the same sequence of numbers.

2) "SK": This is the record interval the numbers are picked with. This insures a very large number of random sequences.

You must use both of these variables to uniquely identify the seed for future access.

3) "MD": This is the upper limit of the number to be generated.

4) "LD": This is the lower limit of the number to be generated.

5) "RD": This is the random digit itself, configured according to the parameters above.

So intially 1,2,5 are passed FROM the routine to your main code and 3 & 4 are passed from YOUR code to the routine. To regain a former sequence you will need to then pass SC & SK to the routine as well.

So here's the code:

1 RANDOMIZE TIMER
600 ' generate random seed sequence
601 GOSUB 750: CLOSE #1: GOSUB 610
603 PRINT "Random number is"; rd$: PRINT : INPUT "[x] to end"; a$: RANDOMIZE TIMER: CLS : IF a$ = "x" THEN END ELSE CLOSE #1: GOSUB 610: GOTO 603
605 ' sk=skip interval, sc=seed counter, ml$, ml=max length
607 ' md=maximum digit value, sd=sample digit, rl=random digit length
609 ' ld=lowest digit value, ll=least digit length, ds=digit seed, rp=record position
610 rd$ = "": rl = 0: sk = INT((1000 - 1 + 1) * RND + 1): sc = INT((100000 - 1 + 1) * RND + 1)
620 ml$ = STR$(md): ml = (LEN(ml$) - 1): ll$ = STR$(ld): ll = (LEN(ll$) - 1)
625 OPEN "seed" FOR RANDOM AS 1: IF ll = ml THEN rl = ll: GET #1, sc-1: GOTO 685 'skip length routine
627 rp = sc - sk
630 FOR i = 1 TO ml' determine random digit length first
640 rp = rp + sk: GOSUB 990: GET #1, rp, sd
660 IF sd > 4 AND rl = 0 THEN rl = (ml - i) + 1
670 NEXT i: IF rl = 0 THEN rl = 1
680 IF rl < ll THEN 630 'if number is too low then re-generate
682 ' generate random digit
685 e = 0: FOR i = 1 TO rl
690 rp = rp + sk: GOSUB 990: GET #1, rp, sd
702 sd = INT(sd): IF sd < 0 THEN sd = sd * -1
705 IF (rl = 1) AND (sd > md OR sd < ld) THEN 690
707 IF rl = 1 THEN rd = sd:RETURN
708 IF i <> 1 THEN 720' 1st digit was acceptable
710 IF sd > VAL(LEFT$(ml$, 2)) AND rl = ml THEN 690 'digit will be max length
712 IF (z = 1 AND i <> ml) OR (i = 2 AND rl = ml AND VAL(MID$(rd$, 2, 1)) = VAL(MID$(STR$(md), 2, 1)) AND VAL(MID$(STR$(md), 3, 1)) = 0) THEN 910
720 rd$ = rd$ + STR$(sd)
725 NEXT i
730 IF VAL(rd$) < ld OR VAL(rd$) > md THEN rd$ = "": z = 0: GOTO 685
740 rd = VAL(rd$): RETURN ' Throws control back to your main code here
750 CLS : INPUT "Maximum digit amount"; md: INPUT "Smallest digit amount"; ld
760 IF sd > md THEN 750
770 RETURN
910 ' Zero handler
915 IF i >= 3 THEN 950' advanced handler for repeating zeros
920 rp = rp + sk: GOSUB 990: GET #1, rp, ss
930 IF ss <= 4 THEN rd$ = "0":GOTO 720
940 rd$ = rd$ + "0": z = 1: GOTO 725
950 IF VAL(MID$(STR$(md), i + 1,1)) <> 0 THEN z = 0: GOTO 720
955 rp = rp + sk: GOSUB 990: GET #1, rp, ss
960 IF ss <= 4 THEN rd$ = rd$ + "0":GOTO 720
970 td$ = "0": FOR j = 2 TO (LEN(rd$) - 1)
980 td$ = td$ + mid$(rd$, j,1): NEXT j: z = 0: rd$ = td$: GOTO 725
990 IF rp > 100000 THEN rp = rp - 100000
1000 RETURN

Sorry for a non-bug free entry; don't ask why and I won't tell ;)

Reyth


0

Related Posts

See More



Response Number 6
Name: Mr. Reyth
Date: June 4, 2005 at 02:08:33 Pacific
Reply:

Ok, the 1st major "whoops" I found with the zero handler is line 970. The for next loop should begin with 3 instead of 2 because the 1st byte in any string will be a "blank". There is probably a slight logical error with the zero handler as evidenced by a very slight "pause" in generating certain numbers. This pause is less than a second in duration. The handler is functioning.

If anyone actually decides to use the above code ( which I seriously doubt ;} ) I will be available for full implementation support.

I kind of like the idea of generating my own seed although [rant on] I have to admit that the more efficient way to handle the problem is upgrade to the better version listed above and nuke the crap out of this whole dang project (a MAJOR sidetrack in my program development) by properly being able to use the randomize statement [rant off].

Reyth


0

Sponsored Link
Ads by Google
Reply to Message Icon

HELP with Novell, VB.NET ... Help needed renaming a fi...



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: Resetting seed w/ out RUN

Qbasic Alarm www.computing.net/answers/programming/qbasic-alarm/6990.html

Copying folders w/out full name www.computing.net/answers/programming/copying-folders-wout-full-name/12106.html

is dos weird re. ascii 13? (c question) www.computing.net/answers/programming/is-dos-weird-re-ascii-13-c-question/1875.html