WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   random() (https://www.wowinterface.com/forums/showthread.php?t=46624)

gmarco 06-06-13 12:33 AM

random()
 
Hi all,

Is possible that the function:

random(1,n)

called with a short delay (let's say 1.5 sec) one from each other will return several time in a row the same value (es y). And this is more visible if "n" is not so much far from 1 so that the numbers to randomize are few.

es. of output I mean (1..4) : 2,2,2,2,2,3

Is possible to add some more entropy to this function or rewrite it in other way (es. scaling the number to something larger ?) to get something like:

1,4,3,3,2,4,1 etc etc ...


Thanks for attention.

p3lim 06-06-13 12:41 AM

Nope, that's the point of the function, it's random, and still unlikely you might get the same number over and over again.

Mind if I ask what you need this for?

humfras 06-06-13 01:22 AM

Every number has the same possibility: 1/(number of values)
In your example (1-4), the chance of having the same number again is 25%, that's quite a lot.

If you want to 'block' a specific number for a given time, you'll need an extra OnUpdate script that handles a blacklist.
Or you fix it with a looping function that loops until the current random number is different to the previous.

zork 06-06-13 03:00 AM

If you want a number to only swap every so and then you can work with microtime.
Just cut of as many numbers on the end as you like.

Basically you can cout out the "seconds" counter ranging from 0..9. If you need some fine tunings you can still do operations on that one.

72345344412222
72345344412222

I used sth similar on a picture swapper that changes pictures with a preset order based on a number calculated by microtime cut-off.

There are tons of other ways though.

ravagernl 06-06-13 09:42 AM

If you dont want to have a number that is following itself (did I explain that correctly:confused:) I'm suggesting something like this:
lua Code:
  1. local random
  2. do
  3.     local rand = math.random
  4.     local x, y
  5.     function random(...)
  6.         repeat
  7.             x = rand(...)
  8.         until x ~= y
  9.         y = x
  10.         return x
  11.     end
  12. end

TSquared 06-06-13 10:05 AM

If you want to constrict your random range you can multiply the random number by the full range (ex - 0-4 is a range of 5 numbers so "myRand= floor(math.random() * 5)")

If you never want a number to go "back-to-back" you check if it's one less the range and if it's lower then the previous value just take your value, if it's higher add one.

If you want a random range that does not repeat until it goes through all the numbers you can make an array with the values in sequence and then sort them randomly. Then just pull them off in sequence.

(FYI - looping with random can take a random amount of time ;)

gmarco 06-06-13 03:08 PM

Quote:

Originally Posted by p3lim (Post 279273)
Nope, that's the point of the function, it's random, and still unlikely you might get the same number over and over again.

Mind if I ask what you need this for?

A little addon that summon a random mount.
The problem is that I press the key and summon mount A, I don't want it and repress A (got same mount :-), again ... again until it changes :-) Ok, it is intended to be random, but what I'd like to remove is the effect to have a lot of times the same number picked ...

The relevant part of code is this:
Lua Code:
  1. local number=random(1,#MyCatMount_mrc[category]);
  2.         local picked=MyCatMount_mrc[category][number]:lower();
  3.         local msg=prgname .. " error: unable to summon \"" .. picked .. "\" from \"" .. category .. "\" category.";
  4.        
  5.         for index=1,GetNumCompanions("MOUNT") do
  6.             local _,name=GetCompanionInfo("MOUNT",index);
  7.             if name:lower()==picked then
  8.                 msg = prgname .. " is summoning... " .. picked;
  9.                 CallCompanion("MOUNT",index);
  10.                 break;
  11.             end
  12.         end

Thanks for your answer anyway.

gmarco 06-06-13 03:21 PM

Thanks very much for all the answers. I have now a clearer idea but I need some more time to check your all answers better.

In the mean time... thanks again at all.

Phanx 06-06-13 04:27 PM

Quote:

Originally Posted by ravagernl (Post 279286)
lua Code:
  1. local random
  2. do
  3.     local rand = math.random
  4.     local x, y
  5.     function random(...)
  6.         repeat
  7.             x = math.random(...)
  8.         until x ~= y
  9.         y = x
  10.         return x
  11.     end
  12. end

I like how you upvalued math.random but then didn't use your upvalue. :p

Dridzt 06-06-13 06:41 PM

This might be helpful, it's along the lines of what's discussed here with some code examples.

Randomizing an array

ravagernl 06-07-13 09:14 AM

Quote:

Originally Posted by Phanx (Post 279310)
I like how you upvalued math.random but then didn't use your upvalue. :p

LOL! I think I forgot. I hope the intention is clear :)

Rainrider 06-07-13 06:25 PM

Best ever: http://xkcd.com/221/

SDPhantom 06-07-13 08:32 PM

Quote:

Originally Posted by Dridzt (Post 279313)
This might be helpful, it's along the lines of what's discussed here with some code examples.

Randomizing an array

I would think it's less taxing on the system to just pick a random index rather than to shuffle the entire table.

At any rate, I think this would be a more efficient way. Instead of repeatedly calling math.random() until it returns a different number, you have it randomize to one less than the max and add one if the random number returned is greater than or equals the last number returned.
Lua Code:
  1. local RandFunc,LastMin,LastMax,LastRand=math.random;
  2. function UniqueRandom(min,max)
  3.     if not max then min,max=1,min; end--    If min given, but not max, assume 1 to X
  4.     if not max then return RandFunc(); end--    if min and max not given, just return float random
  5.     if min<max then return nil; end--   Errors if min greater than max
  6.     if min==max then return min; end--  Equal min and max return that number
  7.  
  8.     if min~=LastMin or max~=LastMax then
  9. --      Initialize our variables or reset if ranges changed
  10.         LastMin,LastMax,LastRand=min,max,RandFunc(min,max);
  11.     else
  12.         local rand=RandFunc(min,max-1);--   Decrease range max by 1
  13.         if rand>=LastRand then rand=rand+1; end--   Add 1 if rand >= last result
  14.         LastMin,LastMax,LastRand=min,max,rand;
  15.     end
  16.     return LastRand;
  17. end



Quote:

Originally Posted by Rainrider (Post 279339)

Good one. Would be better if it returned 1 instead of 4. It would be a more realistic d4 roll then. :D


All times are GMT -6. The time now is 12:28 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI