Page 1 of 1
How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Fri Sep 09, 2011 10:07 pm
by Apo
... 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.

Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Sat Sep 10, 2011 9:35 pm
by Dunnerholl
z.B. könnte man auch eine typisierte liste benutzen

Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Sun Sep 11, 2011 10:16 am
by Apo
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.
Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Sun Sep 11, 2011 12:13 pm
by Graveen
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..).
Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Sun Sep 11, 2011 4:55 pm
by Apo
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;
}
}
}
Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Sun Sep 11, 2011 7:29 pm
by Graveen
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);
Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Sun Sep 11, 2011 9:26 pm
by Apo
Just examples, Graveen.

Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Mon Sep 12, 2011 6:51 am
by Dunnerholl
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" ?
Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Mon Sep 12, 2011 7:44 am
by Graveen
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...
but yay, var is to use with care

Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Mon Sep 12, 2011 9:07 am
by Apo
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>>>>();

Re: How to: Einen zufälligen (random) Index eines Arrays...
PostPosted: Tue Sep 13, 2011 6:10 pm
by Dunnerholl
But Dunny this debate is one of the classical coder's holy war...

let's fight
var = VB
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....
