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. |
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? |
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. |
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. |
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:
|
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 ;) |
Quote:
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:
Thanks for your answer anyway. |
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. |
Quote:
|
This might be helpful, it's along the lines of what's discussed here with some code examples.
Randomizing an array |
Quote:
|
Best ever: http://xkcd.com/221/
|
Quote:
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:
Quote:
|
All times are GMT -6. The time now is 12:28 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI