Help - Search - Members - Calendar
Full Version: [RESOLVED] Getting a random number from DB
StealthBot.net > StealthBot > Scripting and Plugins
Sui(C)ide
I was working on a plugin, and i'm a little stuck. What I would like to do is get a random field from the database I have. There's 4 fields. `Clan`, `Leader`, `Wins`, `Losses`. I would like to get a random clantag from the `Clan` column. The use for this would be so it could "/who clan <clantag" and display the users in the channel. (It's basically a war finding plugin, which can also record stats).

Thanks.
The-Black-Ninja
Put the number of unique (SELECT DISTINCT) tags into an array, get a random number, and apply the number to the array?
Sui(C)ide
I get heaps of errors when I attempt. >.>
I'd need a basic example.
IAreConnection
QUOTE(Sui(C)ide @ Aug 7 2009, 11:19 PM) *

I was working on a plugin, and i'm a little stuck. What I would like to do is get a random field from the database I have. There's 4 fields. `Clan`, `Leader`, `Wins`, `Losses`. I would like to get a random clantag from the `Clan` column. The use for this would be so it could "/who clan <clantag" and display the users in the channel. (It's basically a war finding plugin, which can also record stats).

Thanks.


CODE
SELECT column FROM table
ORDER BY RAND()
LIMIT 1


first result in google...

http://www.google.com/#hl=en&q=mysql+r...G=Google+Search
The-Black-Ninja
Here it is with loops and arrays and stuff:
CODE
Sub Event_Load()

   Randomize
End Sub


Sub whatever

   Set as = conn.Execute("SELECT DISTINCT `clan` FROM `table`")
   temp = vbNullString

   Do Until as.EOF
      temp = temp & as.Fields(0) & "|"
      as.MoveNext
   Loop

   If InStr(temp, "|") > 1 Then
      temp = Left(temp, Len(temp) -1)
      tempArr = Split(temp, "|")
      num = Int(((Ubound(tempArr)+ 1) - (LBound(tempArr))) * Rnd + (LBound(tempArr)))
      randomClan = tempArr(num)
   Else
      AddChat vbRed, "Not enough clans to generate a random number."
   End If
End Sub
Pyro
SELECT `Clan` FROM `my_table` ORDER BY RAND() LIMIT 1

QUOTE(IAreConnection @ Aug 7 2009, 11:47 PM) *
You win.
IAreConnection
QUOTE(The-Black-Ninja @ Aug 7 2009, 11:48 PM) *

Here it is with loops and arrays and stuff:
CODE
Sub Event_Load()

   Randomize
End Sub


Sub whatever

   Set as = conn.Execute("SELECT DISTINCT `clan` FROM `table`")
   temp = vbNullString

   Do Until as.EOF
      temp = temp & as.Fields(0) & "|"
      as.MoveNext
   Loop

   If InStr(temp, "|") > 1 Then
      temp = Left(temp, Len(temp) -1)
      tempArr = Split(temp, "|")
      num = Int(((Ubound(tempArr)+ 1) - (LBount(tempArr))) * Rnd + (LBound(tempArr)))
      randomClan = tempArr(num)
   Else
      AddChat vbRed, "Not enough clans to generate a random number."
   End If
End Sub



Your method is slower and defeats the point of SQL. It's not necessary to get the data right out of the table raw and parse it when the control itself can do it for you. smile.gif
nate
I think I've been over this with him before, I'm pretty sure when he says SQL he means MS Access, so it would be "TOP 1"
The-Black-Ninja
QUOTE(IAreConnection @ Aug 7 2009, 11:51 PM) *
Your method is slower and defeats the point of SQL. It's not necessary to get the data right out of the table raw and parse it when the control itself can do it for you. smile.gif
He wanted an example of my method, I gave it to him.
Sui(C)ide
Mad it worked. Thanks.

Resolved.
Ribose
I have marked this topic as resolved. If you did not receive a satisfactory response, please reply and provide as much detail as possible.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.