PDA

View Full Version : confusion on array and random()


khortez
12-15-2014, 05:18 PM
someone was asking for help on this (probably have an idea of what you're thinking or going to say about that but anyway...)


they wanted to make it so whenever they pressed a key, a random image would be selected every time. I suggested putting the images in an array and using random(); referencing the array list. but what i attempted actually didn't work. and i'm not sure why. any clue on what happened? here is my attempt

i won't use their actual image for this example since they're all strings anyway.


function onCreated(){
temp.images = { "test1", "test2"};
temp.random = int(random(temp.images[0], temp.images[1]));
echo(temp.random);
}


i also tried something a friend of mine did. which was

function onCreated(){
temp.images = { "test1", "test2" };
temp.random = int(random(0, temp.images.size() ));
echo(temp.random);
}

i also put the data in echo directly. but no matter how i did it they all came out as: -1, 0, -1

or: 1, 0.

are those correct outputs? was i wrong in expecting it to output the images name or whats happening?

cbk1994
12-16-2014, 04:34 AM
random(a, b) returns a random number in the range [a, b).

So what you probably want to do is take the number you got in your second example and use it as the index of your array.


function onCreated(){
temp.images = { "test1", "test2" };
temp.random = temp.images[int(random(0, temp.images.size()))];
echo(temp.random);
}

khortez
12-16-2014, 05:48 AM
Suddenly i can see how dumb my second attempt looks. I didn't even refer anything from the array at that point. aside from it's size anyway. but anyways thanks. i can see what i did wrong/should have done.

xAndrewx
12-16-2014, 06:13 AM
Instead of text I would've used your method. Creating 26 images (or however many keys you want) and using int(random(0, 27))

khortez
12-16-2014, 08:29 PM
Instead of text I would've used your method. Creating 26 images (or however many keys you want) and using int(random(0, 27))


Ah. if i understand what you're saying correctly, he wasn't trying to use a lot of the keys. he wanted to use onWeaponFired() for his project. the array was just meant to contain his images names. :P


he was trying to create a bomb that would randomly change images each time he pressed D. using putbomb2();


Note:

just for quick reference, when i say "he" i meant the guy i was trying to help.

MysticalDragon
12-18-2014, 05:37 AM
Suddenly i can see how dumb my second attempt looks. I didn't even refer any, thing from the array at that point. aside from it's size anyway. but anyways thanks. i can see what i did wrong/should have done.

There is also a simpler method then this that will give you the same results.


function onCreated(){
temp.images = { "test1", "test2"};
temp.random = randomstring(temp.images);
echo(temp.random);
}

khortez
12-19-2014, 12:03 AM
There is also a simpler method then this that will give you the same results.


function onCreated(){
temp.images = { "test1", "test2"};
temp.random = randomstring(temp.images);
echo(temp.random);
}


And yet... another little tidbit that was off in cyberspace until this moment.. how do you guys remember all these prefixes and what not?

thanks though i got two methods to use now

cbk1994
12-19-2014, 07:01 AM
There is also a simpler method then this that will give you the same results.


function onCreated(){
temp.images = { "test1", "test2"};
temp.random = randomstring(temp.images);
echo(temp.random);
}


Was randomstring ever fixed? Last post I can find about it is from December 2012 indicating it's still broken:

It's not been fixed, I tried it a few days ago.

If it's still broken, I'd avoid it entirely (even if you're aware of how it's broken). You never know when somebody will change something without realizing you were relying on a broken function.

If it was fixed, I wouldn't use it until v6 is mandatory (or is it already?), since it's broken v5 clientside.

MysticalDragon
12-19-2014, 10:47 AM
Was randomstring ever fixed? Last post I can find about it is from December 2012 indicating it's still broken:



If it's still broken, I'd avoid it entirely (even if you're aware of how it's broken). You never know when somebody will change something without realizing you were relying on a broken function.

If it was fixed, I wouldn't use it until v6 is mandatory (or is it already?), since it's broken v5 clientside.

It's not broking we use it Heavily on delteria, if it's broking please elaborate how? Seems to work fine for me.

OutPut

Weapon/GUI-script tempMD added/updated by Carlito
test2
Weapon/GUI-script tempMD added/updated by Carlito
test1

Mis-Read your response, didn't know it was broking on V5 Clientside. Will test sometime on that theory.

Edit: Also confirmed it works on clientside on V6.
PM: received from (npcserver):Kingdoms (Server)
test1
test2
test2
test2
test2
test1
test2

cbk1994
12-19-2014, 11:56 PM
It's not broking we use it Heavily on delteria, if it's broking please elaborate how? Seems to work fine for me.

Try this on both clientside and serverside. I have no idea if it's still broken, though:


temp.arr = {"foo"};

for (temp.i = 0; temp.i < 10; temp.i++)
echo(temp.i @ ": " @ randomstring(temp.arr));

If it's broken, you'll find that half the time it returns an empty string instead of the only element in the array.

callimuc
12-20-2014, 01:57 PM
And yet... another little tidbit that was off in cyberspace until this moment.. how do you guys remember all these prefixes and what not?

you'll recall them all the time, kinda like learning a new language. also you can find some functions by using /scripthelp keyword on rc, while keyword is some part of the functions name or description. that way you can find some stuff or parameters of a function

MysticalDragon
12-21-2014, 05:06 AM
Serverside Output

Weapon/GUI-script RandomBrokeing added/updated by Carlito
0: foo
1: foo
2: foo
3: foo
4: foo
5: foo
6: foo
7: foo
8: foo
9: foo


ClientSide Output

PM: received from (npcserver):Kingdoms (Server)
0: foo
1: foo
2: foo
3: foo
4: foo
5: foo
6: foo
7: foo
8: foo
9: foo

Works fine

cbk1994
12-21-2014, 04:30 PM
Works fine

Glad to see, and looks like v5 can no longer be downloaded either.