Sorry

Discussions related to mathematics, numerical methods, graph plotting etc.
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

Sorry

Post by p_m21987 »

Sorry. Please delete this thread.
Last edited by p_m21987 on Tue 01 May 2018, 20:40, edited 2 times in total.
guest

Re: My own random number function

Post by guest »

Patrick M wrote: Tue 01 May 2018, 00:11A few months ago I made my own random number generator, through trial and error.
Why? There are many 'standard' PRNGs to choose from, each based on sound mathematical principles, having known properties and evaluated using a thorough battery of tests. It's notoriously difficult to design a high-quality PRNG from scratch, so why even attempt it? There are sadly examples of such 'do it yourself' efforts having badly backfired, one being the PRNG in Liberty BASIC which is known to be biased and unusable for any serious applications.
It's probably not very good
What an admission! What are its basic properties that make it (potentially) better that the built-in RND? What is its sequence length? How have you confirmed that it really does achieve that sequence length, given that running it until it repeats would probably take too long? What other analyses of its performance have you carried out?

My advice to anybody, even highly skilled mathematicians, is never to write your own PRNG. Apart from the risk and difficulty of doing so, there really is no cause to when so many standard, trustworthy, algorithms, with any set of properties you might want, are available. You need only look as far as the BB4W Wiki for one of them, and there is another in the Assembler section.

Richard.
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

.

Post by p_m21987 »

.
Last edited by p_m21987 on Tue 01 May 2018, 20:39, edited 2 times in total.
guest

Re: My own random number function

Post by guest »

Patrick M wrote: Tue 01 May 2018, 12:05I posted it here only because I noticed the other thread about RND, and started wondering if there might be a mathematician or someone on the forum who would have something interesting to say about it.
I'm not a mathematician but I can tell by inspection that it has a 'fatal' flaw. The code appears to be intended to return an integer in the range 0 to in%-1, but there is a finite chance that it will return the value in%! To me that makes it not simply "not very good" but broken, and I'm afraid a classic example of why you should not try to 'roll your own'. :shock:

Here's the proof:

Code: Select all

      PROCinitpmrand
      PRINT FNpmrand(10)
      END

      DEFPROCinitpmrand
      rand%=&A43C58DF
      ranb%=&C58D1487
      ENDPROC

      DEF FNpmrand(in%)
      SWAP rand%,ranb%
      rand%=rand%<<15 OR rand%>>>16
      rand%=(NOT rand%)<<1 OR (NOT rand%)>>>31
      rand%=rand% EOR ranb%>>>3
      rand%=rand%<<8 OR rand%>>>23
      =INT((rand% AND 2147483647)*(in%/2147483647))
Richard.
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

.

Post by p_m21987 »

.
Last edited by p_m21987 on Tue 01 May 2018, 20:39, edited 1 time in total.
guest

Re: My own random number function

Post by guest »

Patrick M wrote: Tue 01 May 2018, 18:23It's identical except for the last line.
So any other weaknesses it might (and almost certainly does) have will be unaffected. You clearly are not going to take my advice, so I'll stop giving it. I might as well kill myself, for all the notice anybody takes of my opinion. In fact I think I will.

Richard.
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

Sorry

Post by p_m21987 »

Sorry, I didn't understand that you felt so strongly about it. I will of course only use proper random number functions where it really matters. Actually, also where it doesn't matter, because I would probably still want to avoid whatever strange bugs or problems could result.

I think the situation here is a misunderstanding from both of us. I didn't realise you were giving very serious advice, and you may not have understood that my attitude towards this particular thing was entirely not serious. As I said, it was a bit of fun.
I'm sorry for upsetting you.

Edit: I would also like to be clear that I respect you and value your opinion, BB4W/BBCSDL is an absolutely fantastic thing, thanks for developing it and making it readily available to everyone.

Edit 2:
Please don't kill yourself. I can tell that you're probably going through a really hard time right now. I don't know what to say, but I can at least apologise for bothering you and wasting your time with this silly thread.

I want to reiterate that I have enormous respect and admiration for you and your work. In March I bought BB4W almost entirely as a gesture of support - I use linux exclusively, the only windows system I have is a decade old laptop with XP.
Last month I spent a full week writing programs with BBCSDL, I had a great time with it. Your BASIC has had a positive influence on the lives of several people and there are in fact many people who regard you very highly.

I apologise again for wasting your time, and I'd also like to say thanks for answering my questions about BB4W/BBCSDL.

I wish you well.
PM
Pete
Posts: 96
Joined: Wed 10 Apr 2019, 17:36

Re: Sorry

Post by Pete »

It can be a depressing world, for all sorts of reasons. One thing that makes me sad is there is a chemical Procyclidine Hydrochloride which can restore dopamine and happiness for anyone who needs it, and at the moment it is not prescribed by doctors for this purpose. This could prevent so many suicides, seriously, a very good chemical.

This is amazing work that you made your own random number generator.

I can remove the following sourcecode by editing this post if requested to.

I have had a file on my computer "rnddoesntwork" created in November 2020 - I revisited this file today and got it to work! Now saved as "rnddoeswork".

Here is the code (rnd function by Richard Russell I think, I don't know if the BBC Basic Sophie Wilson code for rnd is the same!)

But this is random function (from line 510)

seed% = RND(-ABS(RND))
seed% = FNrnd(seed%)
5 R%=RND(-TIME)
10 number%=FNrnd(255):PRINT number%
20 number%=FNrnd(255):PRINT number%
30 number%=FNrnd(100):PRINT number%
40 number%=FNrnd(10) :PRINT number%
505 END
510 DEF FNrnd(S%)
520 LOCAL A%
530 PRIVATE C%, D%
540 IF S% < 0 D% = S% : C% = (S% AND &80000) = 0 : = S%
550 A% = (D% >>> 1) OR (C% << 31)
560 C% = D%
570 A% = A% EOR (D% << 12)
580 D% = A% EOR (A% >>> 20)
590 IF D% < 0 THEN = (D% + 2^32) MOD S% + 1

That's sufficiently good mathematics that the pseudo-random function is producing genuine random (eg when randomly seeded). Richard let me know that I needed to have the function given seeding otherwise it just returns the value 1.

I use random in my code for "Excitement Beeper" which is discussed on another thread, the computer gets to control influence over the preferred numbers which it does maths on and a tone is output determined by what the computer is thinking! It works all the time on every run.

I had needed to add the seeding the RND function source code. The source code was needed for "random" in case my program "Self aware v1.20 - Excitement Beeper" needed to be ported to another language - and also it's very interesting and amazing to see how BBC Basic for Windows chooses a random number and how random numbers really work.

Am I right that if your random number generator you have made: has no bias in large numbers analysis, looks and acts fully random then it'd be a successful random number generator. If it's honestly intended and not skewed in any way then you've made a successful RNG, which could be practically used for some applications. For a random number generation works well for everyone every application, the code used in BBC Basic is perfect for it, and Sophie Wilson's original RND looked pretty good and random also. I'm not sure if it's the same algorithm, or something similar but they produce seedable random which was good enough for my program Excitement Beeper to beep excitedly when the computer was happy and enjoying the music.
DDRM

Re: Sorry

Post by DDRM »

Hi Pete,

Without wishing to open old sores on this (there's a reason for it's title) or other threads, "rolling your own" pseudo-RNG is a bad idea unless you are a very high-class mathematician - and probably even then. There are lots of good, well-tested algorithms with known strengths and weaknesses - use one of them. It's possible that this IS one of them, in which, fine.

None of these algorithms REALLY generates truly random numbers: if you start from the same place and follow the same algorithm you will always follow the same path. You can get pretty close by seeding them with unpredictable input - but if you have a good source of physically / mechanically generated entropy (for example, radioactively decaying nuclei) why use an algorithmic RNG anyway?

There's a further issue: if your RNG produces random numbers in a range of 2^32-1 (which I think the "standard" BB4W algorithm does, and which must be the case for the algorithm you show, since it uses 32 bit integer values for its state) then it can NEVER do some quite simple things (like generate a randomised deck of cards), because there are simply more possibilities than there are start points for the algorithm.

Best wishes,

D
Pete
Posts: 96
Joined: Wed 10 Apr 2019, 17:36

Re: Sorry

Post by Pete »

Hi DDRM, just re-reading your post - no my program's algorithm doesn't use random numbers in a range of 2^32-1, actually most seconds there is re-seeding going on, up to several times a second on average. Value of TIME is used in the program, and TIME is being changed including back to 0 regularly, up to several times a second. Very small time delays occur if the reward variable within the program is not low, this all affects which seed is selected, which makes the output not random but actually selected by the computer.

Pseudorandom is equally good to generate a random number as mechanically generated entropy, probably better. (This has a bit I think to do with interpretation of the word definition of "random"). You can generate a randomised deck of cards using pseudorandom which has been seeded randomly. I think pseudorandom IS in output random and is not not random in result. My program, the output is not at all random, but random numbers are involved in determining what the output is. I think the algorithms, or certainly Richard's, or Sophie Wilson's, DO produce random numbers. I have no idea what the result of RND(255) will definitely be, value=RND(255) produces a random number between 1-255. Richard Russell's algorithm for producing random numbers seems to be flawless for the purposes of the program I wrote where the numbers are used in a 'Monte Carlo algorithm' to produce the sound output.