English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 07 March 2022, 17:28   #1
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Random-Asm in AmiBlitz3?

I'm currently writing a game that randomly places 16 tiles on a 9x9 board. For this I use the Rnd-command of AmiBlitz3. Today I worked a lot on the game and tested it and had to realize that coincidence is not such a given. I got the same game board twice with the exact same position of the chips!

Do I need to initialize anything before using the Rnd-command?

I found some assembler code examples in this thread on how to build a good random number generator. Unfortunately, I have no idea how to integrate the assembler code into AmiBlitz3, for example to generate a word-integer. Can someone show me how I could integrate the intermediate example from the linked thread into AmiBlitz3?

Thank you very much!
AmiNju is offline  
Old 07 March 2022, 17:37   #2
Total Eclipse
Registered User
 
Total Eclipse's Avatar
 
Join Date: May 2005
Location: Atherstone / UK
Age: 49
Posts: 161
I've not used AmiBlitz, but is there an option to provide a seed value to the RND command?

If so, there's possibly a timer value that you can use as the seed, perhaps the number of milliseconds since the system started? Using that before your first call of the RND command in your code should be enough.

* - this is only going on what I remember from Quick Basic on the PC. The command Randomize Timer was good enough to provide a different seed each time the code was run.
Total Eclipse is offline  
Old 07 March 2022, 22:32   #3
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Unfortunately, there are no initialization options. At least I don't see any in the instruction set. The manual doesn't even mention it, too.
AmiNju is offline  
Old 07 March 2022, 23:03   #4
Nightshft
Registered User
 
Nightshft's Avatar
 
Join Date: Mar 2018
Location: Austria
Posts: 617
I was surprised by the missing randomize function, too. I mean, it's even present in the C64 basic.

However, Amiblitz 3 includes elmore_math_lib and with that you can use:
Code:
RRandomize someinteger

or for example

RRandomize Timer

myNumber.w = RRnd(low,high)
;which yields integers from low to high inclusively
Nightshft is offline  
Old 08 March 2022, 08:24   #5
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Funny, very funny...

With this code I get the same number every time too:

Code:
a.w = 0
RRandomize Timer
a = RRnd(1, 10)
NPrint a
END
But, I've found another seed in the guide-files. When I use this with the key word Timer as you mention and using the following code, I get different numbers:

Code:
a.w = 0
Rndseed Timer
a = Rnd(10)
NPrint a
END
Okay, the problem is solved I think. Anyway, a coding language needs a good documentation. I miss this for AB3.

Thank you Total Ecplise and Nightshft!


*edit*

No, problem is not solved. After some tests I get the same number every timer too....wtf?!

Last edited by AmiNju; 08 March 2022 at 08:31.
AmiNju is offline  
Old 08 March 2022, 09:30   #6
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Hm...it seems the initial vector is the problem. For example:

If I use WinUAE in quick configuration (i.e. A500 1MB) and start from floppy my game then I've "Randomize Timer" at the same position in the game - in startup code in this case.

This will produce every time the same seed, because the timer value is the same.

How can I produce a (truly?) unique seed in this case?

No, "Ticks" produce the same seed too. No, placing "Randomize" after the first user interaction will sooner or later produce the same seed too.
AmiNju is offline  
Old 08 March 2022, 11:38   #7
Coagulus
Gets there in the end...
 
Coagulus's Avatar
 
Join Date: Sep 2005
Location: Wales
Posts: 862
I you have a "press to start/continue" then randomise using the timer after that it will most likely be different every time as a human element will have crept in then.

<edit> ah just read your post properly...
Coagulus is offline  
Old 08 March 2022, 12:32   #8
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Quote:
Originally Posted by Coagulus View Post
I you have a "press to start/continue" then randomise using the timer after that it will most likely be different every time as a human element will have crept in then.

<edit> ah just read your post properly...

Your right! I will do the following to get a unique seed:

1. Generate pseudo random numbers on a PC and save them to a file (=random.dat), range 1.000.000 to 9.999.999
2. Generate a pointer file with e.g. 1 as number in this file
3. In AB3 read pointer and increment this number
4. In AB3 save the incremented number (=x) to pointer file
5. In AB3 seek in the random.dat für the position equal to the pointer (eg x * 3). or in other words: Seek for the location x * 3 of random number in the random.dat
6. In AB3 read this random number (then close file)
7. Add Ticks (=time from startup the amiga) to this random number

Result: a "random" seed for the randomizer

This will works for all WinUAE-machines (and Amiga of course) where the start conditions are every time equal.
Yes, you can get the seed with ticks and timers after the first interaction of the user (or another interaction of the user), but the range of this seed is for my game to limited.

For example: If the player push 2 times the button, the game will generate the board with random tiles. The time-range of this 2 times of pushing the button can be under one second. This is a tickrange <= 50.

Thanks to everyone who led me to my mistake in reasoning or contributed to the solution.
AmiNju is offline  
Old 08 March 2022, 13:12   #9
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Quote:
Originally Posted by AmiNju View Post
For example: If the player push 2 times the button, the game will generate the board with random tiles. The time-range of this 2 times of pushing the button can be under one second. This is a tickrange <= 50.
Using ticks or vertical sync in this situation does indeed limit the range of values, but you could instead use the horizontal sync, which runs at around 15kHz, or in other words, gives you a range of around 15,000 values in one second. You can read this counter directly from CIA-B as 3 separate 8-bit values representing a 24-bit value. $BFD800 contains the low byte, $BFD900 contains the middle byte, and $BFDA00 contains the high byte, though for most purposes the high byte would be redundant as most screenmodes have horizontal sync within the 16-bit range.

Full CIA details: http://amigadev.elowar.com/read/ADCD.../node012E.html

Edit: I should point out that this will only really be effective when you aren't synchronising that code with the screen updates, otherwise you'll get values quite close in range each time. If your code is synchronised with the screen refresh, you could use a high resolution timer on the CIA instead.

Last edited by Daedalus; 08 March 2022 at 13:32.
Daedalus is offline  
Old 08 March 2022, 13:40   #10
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Quote:
Originally Posted by Daedalus View Post
Using ticks or vertical sync in this situation does indeed limit the range of values, but you could instead use the horizontal sync, which runs at around 15kHz, or in other words, gives you a range of around 15,000 values in one second. You can read this counter directly from CIA-B as 3 separate 8-bit values representing a 24-bit value. $BFD800 contains the low byte, $BFD900 contains the middle byte, and $BFDA00 contains the high byte, though for most purposes the high byte would be redundant as most screenmodes have horizontal sync within the 16-bit range.

Full CIA details: http://amigadev.elowar.com/read/ADCD.../node012E.html

This should be a good solution too. Thank you for your comment and inspiration! I will test this.

Maybe we should change the topic of this thread to "Get good seed for Rnd() in AB3" ?
AmiNju is offline  
Old 08 March 2022, 13:42   #11
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Quote:
Originally Posted by Daedalus View Post
Edit: I should point out that this will only really be effective when you aren't synchronising that code with the screen updates, otherwise you'll get values quite close in range each time. If your code is synchronised with the screen refresh, you could use a high resolution timer on the CIA instead.

You're right...hum.
AmiNju is offline  
Old 08 March 2022, 14:09   #12
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Using a high resolution CIA timer is pretty easy if you're not worried about multitasking. If you want a system-friendly high resolution timer, check out eclock.include, which is included with AmiBlitz 3.
Daedalus is offline  
Old 08 March 2022, 16:53   #13
AmiNju
Registered User
 
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
Quote:
Originally Posted by Daedalus View Post
Using a high resolution CIA timer is pretty easy if you're not worried about multitasking. If you want a system-friendly high resolution timer, check out eclock.include, which is included with AmiBlitz 3.

Yes, I saw the eclock-include but got error messages that the EasyStruct-type is unknown.
AmiNju is offline  
Old 09 March 2022, 20:10   #14
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
EasyStruct is defined in the system resident files, so if you add amigalibs.res or all.res to your compiler settings, it should work fine. If you load the include file itself in and compile it, there's usually a demo routine at the end of the source. If that works fine but your code doesn't, first thing to check is the resident files selected in the compiler settings of the include.
Daedalus is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASM: Asm-ONE or AsmPro - how to set a Hello amiga coders, I hope it is ok to hijack ? Fireball Coders. Asm / Hardware 2 24 April 2020 21:16
[ASM] Random number generator LeCaravage Coders. Asm / Hardware 7 18 September 2018 15:32
asm code for a simple random generator jotd Coders. Asm / Hardware 7 15 September 2017 09:14
random number generation (in asm) meynaf Coders. General 183 29 November 2010 19:48
Help needed!!Random octal numbers generator(asm) sheryn88 Coders. General 6 01 August 2010 07:19

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 21:40.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.23232 seconds with 15 queries