IPB

Welcome Guest ( Log In | Register )

1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

> [RESOLVED] Getting a random number from DB
Sui(C)ide
post Aug 7 2009, 09:19 PM
Post #1


SuI[C]IdE's DotA Leage Plugin Author
***

Group: Members
Posts: 766
Joined: 23-April 09
From: Australia
Member No.: 49,768



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.


--------------------
IPB Image
Interested in joining an Aus/Nz DotA League? Come by Clan AIDL today, on UWEST.
You may visit our website Here.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
 
Reply to this topicStart new topic
Replies(1 - 10)
The-Black-Ninja
post Aug 7 2009, 09:28 PM
Post #2


Pure Sex
********

Group: Moderators
Posts: 7,756
Joined: 13-August 06
Member No.: 7,372



Put the number of unique (SELECT DISTINCT) tags into an array, get a random number, and apply the number to the array?


--------------------
IPB Image

-My Plugins :: Thanks Pyro :)
IPB ImageIPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Sui(C)ide
post Aug 7 2009, 09:43 PM
Post #3


SuI[C]IdE's DotA Leage Plugin Author
***

Group: Members
Posts: 766
Joined: 23-April 09
From: Australia
Member No.: 49,768



I get heaps of errors when I attempt. >.>
I'd need a basic example.


--------------------
IPB Image
Interested in joining an Aus/Nz DotA League? Come by Clan AIDL today, on UWEST.
You may visit our website Here.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
IAreConnection
post Aug 7 2009, 09:47 PM
Post #4


<3
***

Group: Sr. Members
Posts: 402
Joined: 15-May 06
From: Smoochie's Magic Jungle
Member No.: 4,637



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


--------------------
VBScript or Toshley @ Azeroth
Stuff: BCP 2.0 Website
, PyBot, JBLS Server Status
BCP User Forum <-- don't pm me, post there

IPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
The-Black-Ninja
post Aug 7 2009, 09:48 PM
Post #5


Pure Sex
********

Group: Moderators
Posts: 7,756
Joined: 13-August 06
Member No.: 7,372



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


--------------------
IPB Image

-My Plugins :: Thanks Pyro :)
IPB ImageIPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Pyro
post Aug 7 2009, 09:50 PM
Post #6


huh?
********

Group: Root Administrators
Posts: 9,073
Joined: 23-February 07
From: Orlando, Florida
Member No.: 19,243



SELECT `Clan` FROM `my_table` ORDER BY RAND() LIMIT 1

QUOTE(IAreConnection @ Aug 7 2009, 11:47 PM) *
You win.


--------------------
Pyro
Chieftain of Clan BoT.
Host of pyro.no-ip.biz
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
IAreConnection
post Aug 7 2009, 09:51 PM
Post #7


<3
***

Group: Sr. Members
Posts: 402
Joined: 15-May 06
From: Smoochie's Magic Jungle
Member No.: 4,637



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


--------------------
VBScript or Toshley @ Azeroth
Stuff: BCP 2.0 Website
, PyBot, JBLS Server Status
BCP User Forum <-- don't pm me, post there

IPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
nate
post Aug 7 2009, 09:54 PM
Post #8


Full Member
***

Group: Sr. Members
Posts: 741
Joined: 29-January 06
Member No.: 30



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"
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
The-Black-Ninja
post Aug 7 2009, 09:54 PM
Post #9


Pure Sex
********

Group: Moderators
Posts: 7,756
Joined: 13-August 06
Member No.: 7,372



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.


--------------------
IPB Image

-My Plugins :: Thanks Pyro :)
IPB ImageIPB Image
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Sui(C)ide
post Aug 8 2009, 12:51 AM
Post #10


SuI[C]IdE's DotA Leage Plugin Author
***

Group: Members
Posts: 766
Joined: 23-April 09
From: Australia
Member No.: 49,768



Mad it worked. Thanks.

Resolved.


--------------------
IPB Image
Interested in joining an Aus/Nz DotA League? Come by Clan AIDL today, on UWEST.
You may visit our website Here.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Ribose
post Aug 8 2009, 09:35 AM
Post #11


So, it's all about fame, huh?
*****

Group: Global Moderators
Posts: 2,950
Joined: 13-February 06
From: Connecticut, USA. (GMT -5:00)
Member No.: 74



I have marked this topic as resolved. If you did not receive a satisfactory response, please reply and provide as much detail as possible.


--------------------
~Ribose
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 20th October 2014 - 05:20 AM
Skin by Andrea
Website Legal Information | Hosted by LunarPages