07 March 2022, 17:28 | #1 |
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! |
07 March 2022, 17:37 | #2 |
Registered User
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. |
07 March 2022, 22:32 | #3 |
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.
|
07 March 2022, 23:03 | #4 |
Registered User
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 |
08 March 2022, 08:24 | #5 |
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 Code:
a.w = 0 Rndseed Timer a = Rnd(10) NPrint a END 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. |
08 March 2022, 09:30 | #6 |
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. |
08 March 2022, 11:38 | #7 |
Gets there in the end...
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... |
08 March 2022, 12:32 | #8 | |
Registered User
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
|
Quote:
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. |
|
08 March 2022, 13:12 | #9 | |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
|
Quote:
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. |
|
08 March 2022, 13:40 | #10 | |
Registered User
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
|
Quote:
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" ? |
|
08 March 2022, 13:42 | #11 | |
Registered User
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
|
Quote:
You're right...hum. |
|
08 March 2022, 14:09 | #12 |
Registered User
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.
|
08 March 2022, 16:53 | #13 | |
Registered User
Join Date: Mar 2020
Location: Duesseldorf / Germany
Posts: 32
|
Quote:
Yes, I saw the eclock-include but got error messages that the EasyStruct-type is unknown. |
|
09 March 2022, 20:10 | #14 |
Registered User
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.
|
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 |
|
|