Random Seeds

BlitzPlus Forums/BlitzPlus Programming/Random Seeds

Vic 3 Babes(Posted 2007) [#1]
This is really weird.

I just upgraded to BlitzPlus from Blitz 2D v1.8 and have discovered the following, and was wondering if there was anyone who could give me an explanation.

I know that BlitzPlus doesn't generate the same random numbers from the same seeds as Blitz 2D v1.8, and although this caused a few problems, there's nothing that can't be easily fixed. However, whilst testing some old stuff, I discovered the following:

I wrote a routine called SeedCrypter.bb in Blitz 2D that encrypts a textfile. I wrote this out of curiosity, just to see if it worked, not because I have any use for it.

The encrypter loads the textfile into a bank.
If it's an odd number of bytes, it adds a space to the end.
Then it peeks the first short (which is always within the range $909 to $7E7E) and finds a 2-byte seed that reproduces that short on the first attempt at generating a random number within that range.
It writes the seed into the bank over the old short.
Then it peeks the next short and repeats the process until it gets to the end.
Then it saves the encrypted bank.

The decrypter loads the bank, peeks each short seed and sets SeedRnd.
It then generates the 2-byte random number between $909 and $7E7E, poking it back over the 2-byte seed etc.
Repeating the process for each succesive short in the bank

This works perfectly in Blitz 2D with every text file I've tried (though I have realized recently that the British Pound symbol would be missed if it happened to be in the wrong byte.

However, BlitzPlus hasn't managed to encrypt a single text-file, or even simply phrase. Here is a short version of the code to explain what's going on:

Graphics 640,480,0,2
SetBuffer BackBuffer()

shortascii=Asc("T") Or (Asc("h") Shl 8)
seed=-1
Repeat
seed=seed+1
SeedRnd seed
randnum=Rand($909,$7E7E)
Until (randnum=shortascii) Or (seed=$FFFF)
If randnum=shortascii
Text 0,0,"Seed="+Right$(Hex$(seed),4)
Text 0,15,Chr$(randnum Shr 8)+Chr$(randnum And $FF)
Else
Text 0,0,"No seed found"
EndIf
Flip False
WaitKey()
End

Blitz 2D can easily encrypt this entire textfile using this method - (I'm writing to a textfile, and will copy and paste it into the forum) - but BlitzPlus can't. I've tried both Blitzes on several textfiles - Blitz 2D never fails. Although BlitzPlus can encrypt the first two characters of this textfile "Th" - it can't do the next two "is", or "si" as it would be peeked - the endian format is really irrelevant, since Blitz2D can do it either way around.

It just seems really strange to me that Blitz 2D can encrypt several different textfiles, has never in fact failed to do so, when the different combinations of adjacent letter-placings must be humongous - yet BlitzPlus has never managed to get past 6 bytes in any textfile or phrase that I have tried.

This led me to conclude that BlitzPlus's random number generator doesn't produce as wide a spread of random numbers as Blitz 2D, so I wrote some code to compare the frequency of different random numbers generated in the above range with seeds from 0 to $FFFF. Unless there was a bug in my code, the results were:

Blitz 2D v1.8 - 19563 different numbers
BlitzPlus - 19660 different numbers

The reason for the range in Rand($909,$7E7E) is because the code doesn't expect to come across 2-byte shorts of less than TabTab (ascii 9,9) or greater than ~~ (ascii 126,126). I wanted to keep the range as small as possible to ensure more likelihood of getting a seed when I originally wrote it.

Here is an example of some the phrases I tried with BlitzPlus:

One man went to mow, went to mow a meadow. (failed at position 0)
Now is the Winter of our discontent made glorious Summer by this son of York. (failed at position 2)
I like to program in Blitz because <in 10 words or less> (failed at position 0)

If Depressed then Adjective$="Cruel" else Adjective$="Cool"
Print "Hello, "+Adjective$+" world." (failed at position 2)

I'm going to keep going until I find one that works (failed at position 0)

So statistically speaking, what are the chances - that Blitz2D always produces a seed that works, and BlitzPlus virtually never does. It would suggest a bug in my code - but then why does it work in Blitz 2D, and why does the example code above demonstrate that it doesn't work in BlitzPlus, but works in Blitz 2D?

I really don't care about encrypting files - if I was, I wouldn't tell you the algorithm - but it's doing my head in, and I need to know why it doesn't work. Can anyone see a logical reason?

btw - there were 4569 bytes including the last question mark, and Blitz2D encrypted them all.


Petron(Posted 2007) [#2]
I'm not really sure, but I think it may be with these lines of code. You may want to try revising the rand paremiters, or deleate the perenthisis around the end of the until loop. I hope this helps in any way.

randnum=Rand($909,$7E7E)
Until (randnum=shortascii) Or (seed=$FFFF)


Vic 3 Babes(Posted 2007) [#3]
Sorry - I must be missing something, I can't see how that affects anything - my lines seem correct to me - besides, it works in Blitz v1.8.


Petron(Posted 2007) [#4]
It was just a guess sorry it has no relevance.


Andy_A(Posted 2007) [#5]
It's not you or your code, it's a known bug (for about 3 years now) that has never been resolved.

There's a fellow named Morduun who asked about getting the random number generator seed function to work like Blitz 2D v1.8 he even wrote some code as a work around for this type of situation. Probably in the BlitzCoder archive, can't tell you for sure.


b32(Posted 2007) [#6]
I have only Blitz3D, so this topic is too specific for me to help. However I tried running the program and it works fine. This might be a stupid remark, but have you tried using 'Flip' instead of 'Flip False'? When I tried it, it stopped after 'Th' because of this.


Vic 3 Babes(Posted 2007) [#7]
b32 - Flip False just means flip without waiting for a VBlank.

Andy_A - I remember Morduun from Blitzcoder - but that was before Blitz+. I thought it odd that Blitz2D could generate the right seed without fault, but BlitzPlus couldn't.

Thanks


Amon(Posted 2007) [#8]
I found Morduuns code saved on one of my backup disks. This should be what your looking for, consistant Random Number Generation.

http://www.kamikazekrow.com/storage/mrand.zip

[edit] Man! That was a long time ago. :)

[edit2] I remeber you Vic 3 Babes. I remeber when you did them platform tutorials and everybody kicked up a stink because of the methods you used in the Tutorials. I just wanted to say thanks for those Tutorials. They helped me a lot. :)

Keep Blitzing. :)


Vic 3 Babes(Posted 2007) [#9]
Nice to hear you liked the tutorials - makes the effort seem worthwhile. Some people kicked up a stink because I used so many bitwise operators - still do - always will.

Note the updated 3FF examples in the code archives that came with the tutorials - see Binary Tetris. Coloured bricks are now joined up when they touch - which absolutely requires a knowledge of binary, hex and bitwise operators.

Cheers :)


Petron(Posted 2007) [#10]
Can you post a link to the tutorials?


Vic 3 Babes(Posted 2007) [#11]
Sorry Petron - I don't have Internet access at home, which is why it's taken me 2 days to reply. I have a few tutorials, the current one is nearly a meg when unzipped though.

The platform tutorial is nearly 4 years old, and needs updating for Blitz Plus, which could take a while - especially since I want to finish this current tutorial.

Send me an e-mail if you want me to arrange something though - but I won't read it for a couple of days.


Petron(Posted 2007) [#12]
I have older versions of Blitz anyway. If it is still un the blitzcoder archives could you post it? Thanks


Vic 3 Babes(Posted 2007) [#13]
Sorry - it was never in the BlitzCoder archives - because it was a large on-going project, and everyone just downloaded it, plus updates from my old homepage.

I've just been reading it again this morning - I'm amazed that the example level editor was only about 75 lines of code :)