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.
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.