rand() and switch()

Started by BeckzyBoi, Jun 27, 2018, 10:43 AM

Previous topic - Next topic

BeckzyBoi

rand() and switch() don't seem to work properly in onScriptLoad. Can anyone confirm this?

NicusorN5

Show us how you used them.

BeckzyBoi

A friend has already told me that rand isn't seeded by default, and that it's a bug which causes it to not work. The only problem I am stuck with now is when I use switch() , maybe for the first time?, it's always the last case that is used. When I use if & else everything is fine.

NicusorN5

  Have you used rand() and switch() like this?

local limit = 5;
local value = rand() % limit;
switch(value)
{
  case 0:
  {
   Message("0");
   break;
   }
  case 1:
  {
   Message("1");
   break;
   }
   .
   .
   .
}

BeckzyBoi

Something like that, without the 3 dots. I've scripted in PAWN for 12 years, I know how to use rand(om) and switch. They both definitely don't work properly initially in onScriptLoad.

Thijn

This works absolutely fine for me:
function onScriptLoad()
{
  srand(GetTickCount());
  for ( local i = 0; i<10; i++)
  {
    local rand = rand() % 10;
    print(rand);
  }

  local test = 5;
  switch(test) {
    case 0:
    case 1:
    case 2:
      print("Its 0, 1 or 2");
      break;

    case 5:
      print("Its 5");
      break;

    case 3:
      print("Its 3");
      break;
  }
}

BeckzyBoi

Quote from: Thijn on Jun 27, 2018, 04:24 PMThis works absolutely fine for me:
function onScriptLoad()
{
  srand(GetTickCount());
  for ( local i = 0; i<10; i++)
  {
    local rand = rand() % 10;
    print(rand);
  }

  local test = 5;
  switch(test) {
    case 0:
    case 1:
    case 2:
      print("Its 0, 1 or 2");
      break;

    case 5:
      print("Its 5");
      break;

    case 3:
      print("Its 3");
      break;
  }
}


rand() is working fine for you because you seeded it. It's not like that by default which I believe is a bug? Also for switch - Windows or Linux? 32 or 64 bit? I've got no idea why, but if I use switch() in onScriptLoad, it's always the last case that is used. I only tested by using switch() once, however. It could possibly work the second time of being used, I didn't test that.

Thijn

That depends. I would rather seed it myself, instead of having to rely on a seed that's build in.
I tested switch on windows 32 and 64 bit. If the last case is always being used you're probably forgetting a break; somewhere.