Android Question Long Rnd

sorex

Expert
Licensed User
Longtime User
strange indeed, maybe the multiplying is done by adding and are overflows to negative numbers causing this issue?

you can use something like this instead, Luca.

B4X:
Dim r As String
For x=0 To 20:r=r&Chr(Rnd(0x30,0x39)):Next
log(r)
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
does that work? because the intellisense mentions that rnd returns an int
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
maybe that hint should be modified to something like "it will return a value to the destination's data type limitations" instead of int.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
maybe that hint should be modified to something like "it will return a value to the destination's data type limitations" instead of int.

... and the parameters sholud be "Number", not Int.

I tried these two lines:
B4X:
Private iVar As Int
iVar = Power(2, 32) + 100

and in my opinion they should return an error instead set the value of iVar to the maximum possible.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Luca,

the problem with Power function is similar to the original one.
Power returns a double and adding 100 to it keeps it a double, so right side of assignment has no problem per se.
Casting that double to an integer leads to what you find.
In my opinion is the "silent casting" that could lead to false alarms; personally I would prefer a warning at compile-time rather than an error at runtime.

Umberto
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Ciao Umberto (Ciao should be international :) and it is more nice... hi remember me "Heil..." !)

I do not remember the technical terms in English (and probably not even in Italian)
but the casting should be lawful (G.T. excessive adjective) in one direction and "prohibited" (warned) in the other.
I mean that a Long can contain an Int but not vice versa, if the content of long is greater than the maximum possible content for an int, this should be warned.
----------

The reason for of the immediate error reporting (VB.Net) may also relate to the fact that it is possible to perform calculations in the Immediate window.

I do not know if this is in the versions of B4A next to my (3.20) but it would be enormously helpful. I think, however, would realize that it is very complex.
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
Ciao LucaMs,

se vuoi ricavarti un Long Rnd va bene quello che hai fatto, ma la riga
B4X:
Lng = Int1 * Int2
non ha senso.
Int1 dovrebbero essere 32 bit e Int2 gli altri 32 bit, per cui per abbinarli insieme non devi usare la moltiplicazione, ma uno shift combinato poi con un OR

Se hai bisogno di maggiori chiarimenti chiedi pure

Sergio
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Beh ho usato un metodo diverso, alla fine; nel senso che proprio non uso un long, ma non per la difficoltà incontrata.

Ho capito comunque cosa intendi, ma quella istruzione non dovrebbe dare problemi! Ora la provo con VB.Net.

Grazie

--------------

I've changed my method, I no longer use a long, but not for that problem.

I understand what you mean with a reference to the shift.

However, that statement should be no problem.
 
Upvote 0
Top