Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randomize not very random?
#1
I don't know if this should be in the bug section or is just a general question. There was an older question in the bug forum saying it would be fixed.

Making a card game, I use an array (0 to 51) for card numbers, then have a routine to shuffle it up.
With RANDOMIZE at top of program so it only gets used once each time the game is loaded, I consistently start with 2,0,3,48,9.
Moving it into the "new game" loop so it is run at the start of each new game, I'm always seeing 49,0,21,24.
I'm seeing the same sequence whether I use my Linux or Windows pc.
Not sure if it's something to do with FUSE emulator rather than ZX BASIC, and not using the FRAME setting which the help page mentions?
I've added my source as a txt file for reference.


Attached Files
.txt   hilo3.txt (Size: 4.05 KB / Downloads: 44)
Reply
#2
(04-06-2025, 12:21 PM)BrianT Wrote: Moving it into the "new game" loop so it is run at the start of each new game, I'm always seeing 49,0,21,24.

Move it above do (out of the main loop). There is no reason to call it again when the new game starts.
Still, it shouldn't behave as you said...

Apart from that you should call randomize only once, what comes to mind is this - if you load the game in emulator with some kind of "fast load" (or whatever it is called in fuse), the emulator might reset its state and load quickly, meaning it always take the same time since the Spectrum resets until the programme starts and randomize is called.

I'm not sure if it is that, but might be...
Before solving it, you should test if this is the reason.
To see if it can be the problem, turn off quick load options in emulator and type load"" manually, wait a while and load. If you don't get the same values, then that was it.

If that's what it was, still you want the programme to be used in emulators, so you still need to make it right in emulator when loaded with its quick loading options.
Here is what might solve it - to avoid that - don't call randomize at the start of the programme, but wait some user interaction. That can be when the user choose "H" or "L" for the first time - only then call randomize and shuffle and set some variable like "RandomizeAlreadyCalled = 1", so never call it again - after that you can pick the first card and respond to the user. Then randomize will get different seed, depending on time it takes to the user to take his first choice.

Swan, my ZX Spectrum emulator https://github.com/zoran-vucenovic/swan
Reply
#3
It is exactly as @Zoran commented. You should use RANDOMIZE only once in your program.
If you put it at the beginning of your program, since you're using an emulator and RANDOMIZE relies on timer FRAMES counter to initialize the seed, it will initialize always to the same seed, yielding always the same number sequence.

For a truly "random" seeding, you need an external "random" source, such as user input.
If you ask the user to PRESS a KEY to continue (PAUSE 0), or just use RANDOMIZE after the user press KEY to start the game, then it should work. In this case, you will call RANDOMIZE every time the game is about to start, but it's ok: it will be a differen timer count :-)
---
Boriel
Reply
#4
Thanks @Zoran! I had been using the File/Open (F3) method of auto-loading the file. By instead using Media/Tape/Open (F7) and then LOAD "" , I'm now seeing different results when I run it (and have moved RANDOMIZE back to the top to just be run once).
I hadn't really used that menu option until today.
Reply
#5
(04-06-2025, 08:35 PM)BrianT Wrote: Thanks @Zoran! I had been using the File/Open (F3) method of auto-loading the file. By instead using Media/Tape/Open (F7) and then LOAD "" , I'm now seeing different results when I run it (and have moved RANDOMIZE back to the top to just be run once).
I hadn't really used that menu option until today.

Okay, it's nice you located the problem. Don't forget it's still not solved, as you of course want it to behave correctly even when loaded in an emulator with quick load.
I hope what Boriel and myself wrote is enough for you to workaround this problem -- in short, do not call randomize until you get some user input.

Swan, my ZX Spectrum emulator https://github.com/zoran-vucenovic/swan
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)