Gambas-it

Gambas3 => Programmazione => Topic aperto da: eloaders - 30 Dicembre 2016, 22:38:51

Titolo: Gambas Rnd (Web programming)
Inserito da: eloaders - 30 Dicembre 2016, 22:38:51
I created a sample code:

Codice: [Seleziona]
<%
Dim randoms As String
Dim I As Integer
%>

<html>
<body>
<% For I = 1 To 6%>
<%randoms &= Hex$(Int(Rnd(65536)), 4)%>
<%Next%>
<h1>Gambas web application</h1>
<table border="1" cellpadding="4" cellspacing="0">
  <tr>
    <th><%=randoms%></th>
    <th>Value</th>
  </tr>
  <tr>
    <td></td>
    <td></td>
  </tr>
</table>
<%randoms = Null%>
</body>
</html>

Run with the embedded HTTP server.
each page refresh it gives me the same value, why?
Does anyone know why?
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: vuott - 31 Dicembre 2016, 00:21:23
Hello eloaders,

I have also involved the Spanish forum in your question:

https://foro.gambas-es.org/viewtopic.php?f=1&t=6095


Well, an its member asks why do you use &= instead of = ?
He says: maybe is this the problem ?
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: eloaders - 31 Dicembre 2016, 02:02:15
I tried also with =. It does not work. Always returns the same value. It seems as if RND did not work in this case

However, if you run a script gbw3, then this works. If I run through apache2, nginx, lighttpd, then also works.

Only in the embedded HTTP server does not work

I can also refer to this function
Codice: [Seleziona]
Public Function getrandom() As String
  Dim uniqueid As String
  Dim I As Integer
  For I = 1 To 6
    uniqueid &= Hex$(Int(Rnd(65536)), 4)
  Next
  Return uniqueid
  uniqueid = Null
End

However, any reference to this function returns the same value, only when running is using the embedded HTTP server
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: eloaders - 31 Dicembre 2016, 11:32:00
I sent a report on this page http://gambaswiki.org/bugtracker/main
But they did not accept my report and have it removed.  :bad: :mad:

Codice: [Seleziona]
Public Function getrandom() As String
  Dim uniqueid As String
  Dim I As Integer
  Shell "cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1" Wait To uniqueid
  ' For I = 1 To 6
  '   uniqueid &= Hex$(Int(Rnd(65536)), 4)
  ' Next
  Return uniqueid
End
I tried to use here such code, and it works. Returns me every time the page is loaded another string.

Can also do it in that way:
Codice: [Seleziona]
Public Function getrandom() As String
  Dim uniqueid As String
  Exec ["openssl", "rand", "-hex", "32"] To uniqueid
  Return uniqueid
End
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: Gianluigi - 31 Dicembre 2016, 14:11:34
I sent a report on this page http://gambaswiki.org/bugtracker/main
But they did not accept my report and have it removed.  :bad: :mad:


You must subscribe the mailing list for Gambas users (https://lists.sourceforge.net/lists/listinfo/gambas-user)

However, the notification (http://gambas.8142.n7.nabble.com/Gambas-Bug-Tracker-Bug-1058-Gambas-Rnd-Web-programming-td58102.html) has been accepted  :)
 :ciao:
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: Top Fuel - 31 Dicembre 2016, 22:19:16
Use Randomize keyword before extracting random numbers.
In this way you are sure that each time random number are different.
Like this:

Codice: [Seleziona]
Randomize
For i = 1 To 6
   random = Int(Rnd(65536), 4)
Next
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: vuott - 31 Dicembre 2016, 23:02:46
Use the Randomize keyword ...

eloaders,
also jguardon, like Top Fuel, suggests you to use Randomize:

https://foro.gambas-es.org/viewtopic.php?f=1&p=36503&sid=9c2486e4904ac556bef62f8a8e2d3b68#p36503
Titolo: Re:Gambas Rnd (Web programming)
Inserito da: Top Fuel - 31 Dicembre 2016, 23:44:09
And Rand() function return integer number without using Int() function.