How to: Einen zufälligen (random) Index eines Arrays...

Die deutsche Ecke der Dawn of Light Foren.

Moderators: Support Team, Other Language Team

How to: Einen zufälligen (random) Index eines Arrays...

Postby Apo » Fri Sep 09, 2011 10:07 pm

... ansprechen.

In diesem How to möchte ich dir zeigen, wie du einen zufälligen (random) Index eines Arrays ansprichst.

Wieso solltest du sowas brauchen? Nun, nehmen wir einfach mal an, du möchtest einen eigenen NPC coden und dieser NPC soll nun eines von drei (durch dich) ausgewählten Models verwenden. Da du natürlich die Model-Ids bereits kennst, sparen wir uns den unnötigen Zugriff auf irgendeine Datenbanktabelle und erstellen einfach innerhalb des NPC-Konstruktors einen neuen Array der deine Model-Ids halten wird:
Code: Select all
public MeinNPC()
: base()
{
ushort[] meineModels = new ushort[] { 234, 6322, 632 };
}
Um nun eine zufällige (random) Model-Id auszuwählen, machen wir einfach von der Util Methode 'Random'
gebrauch und weisen dem NPC so ein zufälliges Model zu:
Code: Select all
public MeinNPC()
: base()
{
ushort[] meineModels = new ushort[] { 234, 6322, 632 };

Model = meineModels[Util.Random(0, meineModels.Length - 1)];
}
Das wars auch schon!

Gruß

Apo

p.s. Die Optimierung dieses Codes überlasse ich dir selbst, du sollst ja schließlich auch selbst etwas lernen. ;-)
Apo
Contributor
 
Posts: 341
Joined: Sun May 22, 2005 10:21 pm
Location: Germany

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Dunnerholl » Sat Sep 10, 2011 9:35 pm

z.B. könnte man auch eine typisierte liste benutzen :)
Dunnerholl
Developer
 
Posts: 1229
Joined: Mon Sep 08, 2008 8:39 pm

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Apo » Sun Sep 11, 2011 10:16 am

z.B. könnte man auch eine typisierte liste benutzen :)
Code: Select all
namespace Irgendwas
{
public class MeinNPC : GameNPC
{
private List<ushort> m_meineModels = new List<ushort>();

public MeinNPC()
: base()
{
m_meineModels.AddRange(new ushort[] {234, 6322, 632});

Model = m_meineModels[Util.Random(0, meineModels.Count - 1)];
}
}
}
Wie immer, handelt es sich dabei nur um ein Beispiel.
Apo
Contributor
 
Posts: 341
Joined: Sun May 22, 2005 10:21 pm
Location: Germany

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Graveen » Sun Sep 11, 2011 12:13 pm

In addition, i introduced an extension method for myString.SplitCSV(args). This is targetted to various serialized csv fields in the database, such as the ones in npctemplate.

It returns a list<string>, then, you can apply the same Util.Random(0, max-1) to grab a random one (returned as string, need to be converted in case of int, double etc..).
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12661
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Apo » Sun Sep 11, 2011 4:55 pm

In addition, i introduced an extension method for myString.SplitCSV(args). This is targetted to various serialized csv fields in the database, such as the ones in npctemplate.

It returns a list<string>, then, you can apply the same Util.Random(0, max-1) to grab a random one (returned as string, need to be converted in case of int, double etc..).
Code: Select all
namespace Irgendwas
{
public class MeinNPC : GameNPC
{
public MeinNPC()
: base()
{
int randomModel = -1;

var dbo = GameServer.Database.SelectObject<DBNpcTemplate>("TemplateId = '1234'");
if (dbo != null)
{
var result = Util.SplitCSV(dbo.Model);
if (result.Count > 0)
{
try
{
randomModel = Convert.ToInt32(result[Util.Random(0, result.Count - 1)]);
}
catch
{
List<int> corruptIndexes = new List<int>();

for(int i = 0; i < result.Count; i++)
{
try
{
Convert.ToInt32(result[i]);
}
catch
{
corruptIndexes.Add(i);
}
}

if (corruptIndexes.Count < result.Count)
{
int temporaryIndex = Util.Random(0, result.Count - 1);

do
{
temporaryIndex = Util.Random(0, result.Count - 1);
}
while (corruptIndexes.Contains(temporaryIndex));

randomModel = Convert.ToInt32(result[temporaryIndex]);
}
}
}
}

if (randomModel == -1)
randomModel = 5678; // DE: Standard Model | EN: Default model

Model = (ushort)randomModel;
}
}
}
Apo
Contributor
 
Posts: 341
Joined: Sun May 22, 2005 10:21 pm
Location: Germany

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Graveen » Sun Sep 11, 2011 7:29 pm

it seems very laborious, here is how i handle it in the core:
Code: Select all
ushort choosenModel = 1; // Default Model
var splitModel = template.Model.SplitCSV(true);
ushort.TryParse(splitModel[Util.Random(0,splitModel.Count-1)], out choosenModel);
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12661
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Apo » Sun Sep 11, 2011 9:26 pm

Just examples, Graveen. :)
Apo
Contributor
 
Posts: 341
Joined: Sun May 22, 2005 10:21 pm
Location: Germany

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Dunnerholl » Mon Sep 12, 2011 6:51 am

it seems very laborious, here is how i handle it in the core:
Code: Select all
ushort choosenModel = 1; // Default Model
var splitModel = template.Model.SplitCSV(true);
ushort.TryParse(splitModel[Util.Random(0,splitModel.Count-1)], out choosenModel);
why do you like "var" ?
Dunnerholl
Developer
 
Posts: 1229
Joined: Mon Sep 08, 2008 8:39 pm

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Graveen » Mon Sep 12, 2011 7:44 am

in this case ? because it eases the readability. I 'd not have used it for choosenModel in example.
But Dunny this debate is one of the classical coder's holy war... :mrgreen:

but yay, var is to use with care :)
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12661
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Apo » Mon Sep 12, 2011 9:07 am

And it looks much better than
Code: Select all
Dictionary<string, Dictionary<int, Dictionary<Type, Dictionary<string, object>>>> blub = new Dictionary<string, Dictionary<int, Dictionary<Type, Dictionary<string, object>>>>();
Code: Select all
var blub = new Dictionary<string, Dictionary<int, Dictionary<Type, Dictionary<string, object>>>>();
:)
Apo
Contributor
 
Posts: 341
Joined: Sun May 22, 2005 10:21 pm
Location: Germany

Re: How to: Einen zufälligen (random) Index eines Arrays...

Postby Dunnerholl » Tue Sep 13, 2011 6:10 pm

But Dunny this debate is one of the classical coder's holy war... :mrgreen:
let's fight :D

var = VB :P

so far i've seen people use var mainly from lazyness or because they don't know what to expect, like object....
just interested to find out for what it is actually good, same question as linq, EF, async.... :)
Dunnerholl
Developer
 
Posts: 1229
Joined: Mon Sep 08, 2008 8:39 pm


Return to “%s” Deutsch

Who is online

Users browsing this forum: No registered users and 0 guests