Page 1 of 1

PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:02 am
by codeprof
Hello Fred,

lst(Random(255))+1 seems to be extended to
lst(Random(255)) ) = lst(Random(255)) +1
which leads to really strange behaviour.

Code: Select all

Dim lst(255)
For t=0 To 100000
  lst(Random(255))+1
Next  

For t=0 To 255
  Debug lst(t)
  sum + lst(t)
Next  

Debug "sum (should be 100001):"+Str(sum)
For a "For"-loop there is a similar behaviour (although not so problematically), because the "To-conditions" is evaluated more than once.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:09 am
by uwekel
Strange - this way it works:

Code: Select all

Dim lst(255)
For t = 0 To 100000
  i = Random(255)
  lst(i) + 1
Next

For t = 0 To 255
  Debug lst(t)
  sum + lst(t)
Next

Debug "sum (should be 100001):" + Str(sum)

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:13 am
by eesau
IIRC this has been discussed before, and the verdict was no bug.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:15 am
by Little John
Yes, this can cause problems. It has been discussed before and is by design, no bug.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:33 am
by codeprof
really sad that the purebasic team is not willing to fix this obvious bug... :?
old bugreport

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:42 am
by Little John
codeprof wrote:really sad that the purebasic team is not willing to fix this obvious bug... :?
http://www.purebasic.fr/english/viewtop ... 10#p379010
http://www.purebasic.fr/english/viewtop ... 34#p407434

If you want this to get changed, you can make a feature request.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 11:53 am
by Thade
Where do you see a bug?

Code: Select all

lst(Random(255))
standing alone is senseless - you have to assign something

Code: Select all

lst(Random(255))= variable
or

Code: Select all

lst(Random(255))=1+variable
In PB instead of using

Code: Select all

lst(Random(255))+=1
as in other (script)languages you can use

Code: Select all

lst(Random(255))+1
A great feature!
RGR

.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 12:16 pm
by codeprof
The problem is that e.g.

Code: Select all

lst(Random(255))+1
the compiler replaces Random() with two different random numbers.
So it will be replaced e.g. by:

Code: Select all

lst(55)= lst(201)+1
However this not really obviously.
It shoud be:

Code: Select all

lst(55)= lst(55)+1

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 12:27 pm
by Thade
codeprof wrote:The problem is that e.g.

Code: Select all

lst(Random(255))+1
the compiler replaces Random() with two different random numbers.
So it will be replaced e.g. by:

Code: Select all

lst(55)= lst(201)+1
However this not really obviously.
It shoud be:

Code: Select all

lst(55)= lst(55)+1
No it should not!
If you want to add 1 to a certain array you must program it right

Code: Select all

x=random(255)
lst(x)+1
you cannot expect that

Code: Select all

lst(Random(255))=lst(Random(255))+1
uses the same random number

As I said before var+1 in PB replaces var+=1 in other languages which is precompiled to var=var+1 or Array(x)+1 gets Array(x)=Array(x)+1
or lst(Random(255))+1 gets lst(Random(255))=lst(Random(255))+1

RGR

Edited:
A feature I don't want to miss in the future!

.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 12:48 pm
by codeprof
But PB is the only programming language I know which is doing it this way.
Others like C,C# won't call Random() twice.

Re: PB 5.11b1 bug with short value assignment

Posted: Sun Mar 10, 2013 12:56 pm
by Thade
And we need Mister CodeProfessor to show us the right way?
I work since 2003 with PB - you joined 2012
I am glad with it 10 years now - you obviously not (see the other feature request thread you just started)

No comment anymore
RGR

Re: PB 5.11b1 bug with short value assignment

Posted: Mon Mar 11, 2013 12:51 am
by luis
codeprof wrote:But PB is the only programming language I know which is doing it this way.
Others like C,C# won't call Random() twice.
I agree with you it is strange and I never expected for it to work that way from previous experiences with other languages, the documentation IMO is misleading and routinely someone come up with this. Nevertheless the official explanation was it was made so by design, so you just have too keep in mind the way it actually works. At this point I don't think is going to change.

If you want to have more fun reading about this just follow the link I just posted here -> http://www.purebasic.fr/english/viewtop ... 80#p407480

Re: PB 5.11b1 bug with short value assignment

Posted: Thu Mar 13, 2025 11:42 pm
by Demivec

Re: PB 5.11b1 bug with short value assignment

Posted: Fri Mar 14, 2025 12:04 am
by User_Russian
For some reason the is called twice Random().

Code: Select all

// For t=0 To 100000
v_t=0;
while(1) {
if (!(((integer)100000LL>=v_t))) { break; }
// lst(Random(255))+1
integer rr1=PB_Random(255LL);
integer rr2=PB_Random(255LL);
((integer*)a_lst.a)[(integer)rr2]=(((integer*)a_lst.a)[(integer)rr1]+1);
// Next  
next1:
v_t+=1;
}
il_next2:;