RANDOM() a litle bit more random please...

Everything else that doesn't fall into one of the other PB categories.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by FAKEFACTORY.

Hi,

i there any way to make more random random-numbers out of the RANDOM() function? I.e. RANDOM(7)+1 for random numbers between 1 and 8 gives me
mainly:

1,1,1,1,5,5,5,1,5,5,5,1,....

This is a little bit to static for a random generator. Can we "load" the function with a randomizer-seed, like CurrentTicks? Or is there another Random-generator sourcecode avaiable?


Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi, try this code:

Code: Select all

    Global result.l

    max.l
    seed.l

!Extern _GetTickCount@0
!call _GetTickCount@0
!mov [v_seed], eax

Procedure.l MyRandom(max.l) ; don't use 0 as max, or fires a division by 0 error (avoidable, but would make it slower)
    !mov [v_max], eax
    !mov eax, [v_seed] 
    !mov dword ecx, $41c64e6d 
    !mul ecx 
    !add dword eax, $3039 
    !And dword eax, $7ffffff 
    !mov [v_seed], eax 
    !mov ecx, [v_max]
    !sub edx, edx 
    !div ecx 
    !mov [v_result], edx
    ProcedureReturn result
EndProcedure

result = MyRandom(7)
MessageRequester("Random result:", Str(result), 0)

Bye,

El_Choni

Edited by - El_Choni on 03 February 2002 17:23:47
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Excellent procedure El_Choni !!

Just a note, make sure you reseed off the system clock or something... else you will receive the same set of random numbers every time you restart your program.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

"reseed off"? I don't understand. the GetTickCount call is used to get a different seed each time, precisely to avoid what you say.

I got this code from the Win32asm message board. There are several other pseudorandom number generators there, check it if you wish.

Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi El_Choni
Many many thanks for your suppport to optimize the Random() and Str() commands... The new Random() works very fine... I must test the Str() replace... Anyway many thanks for the friendly and fast support - now my program is a bit smaller :wink:) Would be nice to see the feature in next PB where danilo told about... Link only needed stuff to a file (obj) ... :wink:) This would be very welcome Fred! :wink:

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

You have since edited the version posted.
The version I copy and pasted had a set seed (seed.l=5555), it did not have the GetTickCount.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by DarkUni.

We should probably get Fred to add a RANDOMIZE command native.

Then we could the system timer to randomize easier.

This is definitely vital to video game coding.

Shane R. Monroe
Dark Unicorn Productions
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.
You have since edited the version posted.
The version I copy and pasted had a set seed (seed.l=5555), it did not have the GetTickCount.
He, he, I saw my error and corrected it, but thought that your post was made after the correction.

Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by FAKEFACTORY.

@El_Choni
Great code! Many thanks

Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.
is this for any OS? -jb
sorry... the lib will be coded in pure x86 code... also it dont want run on 680x0 amiga os for example...

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Tron, if you talk about the randomizing procedure, it must be easy to translate (anyway, PB Amiga will be updated soon and will likely include the Random() function, I guess).

Here's a rough translation using 86TO68:

Code: Select all

Data:

seed: dc.l 0

Code:

PB_InitRandom:
 jsr  _GetTickCount@0 ; this should be changed to some Amiga API function which puts the result in d0
 move.l d0,seed
 rts

PB_Randomize:
 move.l d0,-(a7)
 move.l seed(pc),d0
 move.l #1103515245,d2
 add.l  #12345,d0
 and.l  #134217727,d0
 move.l d0,seed
 move.l (a7)+,d2
 sub.l  d3,d3
 move.l d0,d5
 move.l d3,d0
 move.l d5,d3
 rts
See you,


Edited by - El_Choni on 04 February 2002 18:44:46
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

I'm quite sure it won't work without testing and correcting, which I can't do because I lost my WinUAE instalation and my A4000/40 is 600 Km. away.

But it's a start :wink:

Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> I lost my WinUAE instalation

I just installed the latest WinUAE and wow: it's so FAST now! I can
actually play Alien Breed 3D at a respectable speed! Very good work
by whoever added the JIT code to it.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

I think that what is so fast now is your computer :wink:. I'm going to install it again, but don't know where to find a good assembler to test things, and CygnusEd, (oh, well, and so much other things that don't come with UAE, of course).

Have a nice day,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

search for PhxAss.. and GoldED.

Fred - AlphaSND
Post Reply