Jump to content
Unity Insider Forum

[Console] Rarität-Chance eines Items testen


Triky313

Recommended Posts

Hay Leute,

habe hier mal ein kleines Test-Script geschrieben.

Ich wollte die Raritäts-Chance eines Items testen.

 

Habe auf Arbeit kein Unity installiert, deswegen nur eine Konsolenanwendung.

 

Sollte alles selbsterklärend sein. Je nachdem wie sich das PlayerLevel erhöht, wird die Chance für bestimmte Raritäten erhöht. Genauso bei ExtraChance, welche man auf Items vergeben kann.

 

Natürlich muss der nächst niedrigere Wert immer weniger sein, sonst kommt es zu Problemen.

 

Meint ihr, dass das eine gute Lösung ist oder habe ich etwas übersehen, gibt es da vielleicht bessere Ansätze?

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp {
   class Program {
    static void Main(string[] args) {
	    Console.WriteLine("RandomCalculator");
	    while(true) {
		    Console.Write("Do you want to play again [Y/N]?");
		    string answer = Console.ReadLine().ToUpper();
		    if(answer == "Y")
			    for(int i = 0; i < 80; i++) {
				    RandomRarity(0, 0, 0.001f, 0.05f, 1.5f, 25.0f, 50.0f);
			    }
		    if(answer == "N")
			    break;
	    }

    }
    // 0.001% - 100%
    private static void RandomRarity(int playerLevel, int extraChance, float mysticChance, float legendaryChance, float epicChance, float rareChance, float uncommonChance) {
	    int maxRandomValue = 100000;
	    int chanceMultiply = maxRandomValue / 100;
	    playerLevel = (playerLevel > 100) ? 100 : playerLevel;
	    extraChance = (extraChance > 1000) ? 1000 : extraChance;
	    int rndValue = GetRandomNumber(0, maxRandomValue);
	    if(rndValue < (mysticChance * chanceMultiply)) {
		    Console.ForegroundColor = ConsoleColor.Red;
		    Console.WriteLine("MysticItem");
		    Console.ResetColor();
	    } else if(rndValue < (legendaryChance * chanceMultiply) + extraChance + playerLevel) {
		    Console.ForegroundColor = ConsoleColor.Yellow;
		    Console.WriteLine("LegendaryItem");
		    Console.ResetColor();
	    } else if(rndValue < (epicChance * chanceMultiply) + extraChance + playerLevel) {
		    Console.ForegroundColor = ConsoleColor.Magenta;
		    Console.WriteLine("EpicItem");
		    Console.ResetColor();
	    } else if(rndValue < (rareChance * chanceMultiply) + extraChance + playerLevel) {
		    Console.ForegroundColor = ConsoleColor.Blue;
		    Console.WriteLine("RareItem");
		    Console.ResetColor();
	    } else if(rndValue < (uncommonChance * chanceMultiply) + extraChance + playerLevel) {
		    Console.ForegroundColor = ConsoleColor.Green;
		    Console.WriteLine("UncommonItem");
		    Console.ResetColor();
	    } else {
		    Console.ForegroundColor = ConsoleColor.White;
		    Console.WriteLine("CommonItem");
		    Console.ResetColor();
	    }
    }
    private static readonly Random getrandom = new Random();
    private static readonly object syncLock = new object();
    public static int GetRandomNumber(int min, int max) {
	    lock(syncLock) {
		    return getrandom.Next(min, max);
	    }
    }
   }

}

Link zu diesem Kommentar
Auf anderen Seiten teilen

Wenn du es so machst, verdoppelst du quasi die LegendaryChance ab Level 50 (und 3fach ab Level 100). Ansonsten sieht soweit ganz gut aus ;)

 

Ich würde den Randomgenerator noch zufällig initialisieren (z.b. Start() + Time.time oder ), sonst droppen immer die gleichen Items:

https://docs.unity3d....InitState.html

 

oder Sekunden seit 01.01.1970:

http://answers.unity3d.com/questions/417939/how-can-i-get-the-time-since-the-epoch-date-in-uni.html

Link zu diesem Kommentar
Auf anderen Seiten teilen

So, hier mal eine neue Version und diesmal habe ich das Level und die Bonus-Chane als prozentuale Erhöhung genommen. Im Prinzip ist es gar nicht schlecht wenn sich die Chance verdoppelt oder noch höher ist. Somit fallen am Ende auch schlechte Items komplett heraus.

 

Das sollte doch so brauchbar sein, auch von der Performance oder?

 

using System;
namespace RandomRarity {
   class Program {
    static void Main(string[] args) {
	    Console.WriteLine("RandomCalculator");
	    while(true) {
		    Console.Write("Lauf starten [Y/N]?");
		    string answer = Console.ReadLine().ToUpper();
		    if(answer == "Y") {
			    Console.Write("Anzahl Durchlaufe?");
			    int runs = Convert.ToInt32(Console.ReadLine());
			    Console.Write("PlayerLevel?");
			    int pLevel = Convert.ToInt32(Console.ReadLine());
			    Console.Write("ExtraChance?");
			    int eChance = Convert.ToInt32(Console.ReadLine());
			    RandomRarityStaticsticRun(runs, pLevel, eChance, 0.001f, 0.01f, 1.0f, 10.0f, 25.0f);
		    }
		    if(answer == "N")
			    break;
	    }
    }
    // 0.001% - 100%
    private static int RandomRarity(int playerLevel, int extraChance, float mysticChance, float legendaryChance, float epicChance, float rareChance, float uncommonChance) {
	    int maxRandomValue = 100000;
	    int chanceMultiply = maxRandomValue / 100;
	    playerLevel = (playerLevel > 500) ? 500 : playerLevel;
	    extraChance = (extraChance > 1000) ? 1000 : extraChance;
	    int rndValue = GetRandomNumber(0, maxRandomValue);
	    if(rndValue < (mysticChance * chanceMultiply) / 100 * (100 + extraChance + playerLevel)) {
		    return 5;
	    }
	    if(rndValue < (legendaryChance * chanceMultiply) / 100 * (100 + extraChance + playerLevel)) {
		    return 4;
	    }
	    if(rndValue < (epicChance * chanceMultiply) / 100 * (100 + extraChance + playerLevel)) {
		    return 3;
	    }
	    if(rndValue < (rareChance * chanceMultiply) / 100 * (100 + extraChance + playerLevel)) {
		    return 2;
	    }
	    if(rndValue < (uncommonChance * chanceMultiply) / 100 * (100 + extraChance + playerLevel)) {
		    return 1;
	    }
	    return 0;
    }
    private static readonly Random getrandom = new Random();
    private static readonly object syncLock = new object();
    public static int GetRandomNumber(int min, int max) {
	    lock(syncLock) {
		    return getrandom.Next(min, max);
	    }
    }
    public static void RandomRarityStaticsticRun(int runs, int playerLevel, int extraChance, float mysticChance, float legendaryChance, float epicChance, float rareChance, float uncommonChance) {
	    int common = 0;
	    int uncommon = 0;
	    int rare = 0;
	    int epic = 0;
	    int legendary = 0;
	    int mystic = 0;
	    for(int i = 0; i < runs; i++) {
		    switch(RandomRarity(playerLevel, extraChance, mysticChance, legendaryChance, epicChance, rareChance, uncommonChance)) {
			    case 5:
			    mystic++;
			    break;
			    case 4:
			    legendary++;
			    break;
			    case 3:
			    epic++;
			    break;
			    case 2:
			    rare++;
			    break;
			    case 1:
			    uncommon++;
			    break;
			    case 0:
			    common++;
			    break;
			    default:
			    Console.WriteLine("Fehler");
			    break;
		    }
	    }

	    int total = mystic + legendary + epic + rare + uncommon + common;
	    Console.WriteLine("######################### ");
	    Console.WriteLine("Total:\t " + total);
	    Console.WriteLine("Bonus %-Chance:\t " + (playerLevel + extraChance) + "%");
	    Console.WriteLine("######################### ");
	    Console.ForegroundColor = ConsoleColor.Red;
	    float totalMystic = (100.00f / total) * mystic;
	    Console.WriteLine("MysticItem:\t " + mystic + " - " + totalMystic + "%");
	    Console.ResetColor();
	    float totalLegendary = (100.00f / total) * legendary;
	    Console.ForegroundColor = ConsoleColor.Yellow;
	    Console.WriteLine("LegendaryItem:\t " + legendary + " - " + totalLegendary + "%");
	    Console.ResetColor();
	    float totalEpic = (100.00f / total) * epic;
	    Console.ForegroundColor = ConsoleColor.Magenta;
	    Console.WriteLine("EpicItem:\t " + epic + " - " + totalEpic + "%");
	    Console.ResetColor();
	    float totalRare = (100.00f / total) * rare;
	    Console.ForegroundColor = ConsoleColor.Blue;
	    Console.WriteLine("RareItem:\t " + rare + " - " + totalRare + "%");
	    Console.ResetColor();
	    float totalUncommon = (100.00f / total) * uncommon;
	    Console.ForegroundColor = ConsoleColor.Green;
	    Console.WriteLine("UncommonItem:\t " + uncommon + " - " + totalUncommon + "%");
	    Console.ResetColor();
	    float totalCommon = (100.00f / total) * common;
	    Console.ForegroundColor = ConsoleColor.White;
	    Console.WriteLine("CommonItem:\t " + common + " - " + totalCommon + "%");
	    Console.ResetColor();
    }
   }
}

post-6006-0-96757800-1485421782_thumb.png

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...