根据另一个随机值从表中选择一个随机值

时间:2015-07-13 01:12:26

标签: random lua

我正在把我朋友的所有武器统计列表用于他正在运行的RP并将其转换为生成器,但我有问题排除基于其他值的某些值。例如,我不想要二级自动步枪。有没有办法可以根据另一个输出排除某些值?

CategoryLoaded = [[NSUserDefaults standardUserDefaults] integerForKey:@"CategorySaved"];

NSMutableArray *testArray = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat1 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat2 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat3 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat4 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat5 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat6 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat7 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat8 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat9 = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *cat10 = [[NSMutableArray alloc] initWithCapacity:1];


switch(CategoryLoaded)
{
    case 1: testArray = cat1;
        break;
    case 2: testArray = cat2;
        break;
    case 3: testArray = cat3;
        break;
    case 4: testArray = cat4;
        break;
    case 5: testArray = cat5;
        break;
    case 6: testArray = cat6;
        break;
    case 7: testArray = cat7;
        break;
    case 8: testArray = cat8;
        break;
    case 9: testArray = cat9;
        break;
    case 10: testArray = cat10;
        break;
}

int test = arc4random() %17;
while([testArray containsObject: @"test"])
{
    test = arc4random()%17;
}

QuestionSelected = test;

1 个答案:

答案 0 :(得分:3)

问题在于条件:

if 'Primary' then

将始终评估为true,因为除了falsenil之外,任何值都会被评估为真。

你想要的是:

local rand_class = myclasses[math.random(#myclasses)]
print(rand_class)
if rand_class == 'Primary' then
  print( myprimaries[math.random(#myprimaries)] )
elseif rand_class == 'Secondary' then
  print( mysecondaries[math.random(#mysecondaries)] )
end

并且不要忘记随机播种。