SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 17/12/2016, 12:44 PM   #1
coool
Gangsta
 
coool's Avatar
 
Join Date: Feb 2013
Location: Republic of Pakistan
Posts: 625
Reputation: 54
Default How to create randoms with exceptions

I have created a Random function But It sometimes or frequently outputs the same result because I have just 4 things to get a random of.
Is there any way to create a random with exception For ex:I create a random function but how to make exceptions like my random result is 2nd variable and how to prevent it from being the same....

Simply, there are 4 numbers to get random of.... The random results 1. so how to prevent from being one again in result.
__________________
coool is offline   Reply With Quote
Old 17/12/2016, 12:54 PM   #2
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,917
Reputation: 258
Default Re: How to create randoms with exceptions

I did that but I think you can optimize it.
PHP Code:
main()
{
    
printf("%i"randomEx(5_01245)); // print 3
}

randomEx(maxmin 0, ...)
{
    new
        
maxarg numargs(),
        
nb random(max min) + min;

    while(
maxarg != 0)
    {
        if(
nb == getarg(maxarg))
        {
            
maxarg numargs();
            
nb random(max min) + min;
        }
        else
            
maxarg--;
    }
    
    return 
nb;

__________________
actor_plus - v5.0.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is offline   Reply With Quote
Old 17/12/2016, 03:47 PM   #3
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,737
Reputation: 583
Default Re: How to create randoms with exceptions

PHP Code:
randomex(maxexception = -1)
{
if(
exception == -1) return random(max);

new 
random(max 1);

return (
== exception r);

Way simpler than the one above. But only works for preventing the same value 2 times. This can still happen: 2, 3, 2, 3, 2, 3 etc.

If your first result was 2 (out of 0-3), it will get a random value of 0-2. If the value is your last value, it will add one, else take that value. If the first value was the maximum value, it cannot be chosen again (hence "max - 1").
NaS is offline   Reply With Quote
Old 17/12/2016, 03:58 PM   #4
coool
Gangsta
 
coool's Avatar
 
Join Date: Feb 2013
Location: Republic of Pakistan
Posts: 625
Reputation: 54
Default Re: How to create randoms with exceptions

OK, But will this work or not??
Quote:
new rand = Random(5); //1st random result.
new rand2 = Random(5); //2nd random result.
while (rand2 == rand) {
rand2 = Random(5); //continue randoming it until 1st and 2nd results are not same.
}
__________________
coool is offline   Reply With Quote
Old 17/12/2016, 04:07 PM   #5
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,768
Reputation: 754
Default Re: How to create randoms with exceptions

Use an array, something like:
pawn Code:
new array = {0, 1, 2, 3, 4}, count = sizeof(array), selected[5];
selected[0] = random(count);
array[selected[0]] = array[-- count];
selected[1] = random(count);
array[selected[1]] = array[-- count];

and so on...

Note that this example is only for consecutive numbers, you'd have to write your own version if you'd like to use any value.
SickAttack is offline   Reply With Quote
Old 17/12/2016, 04:56 PM   #6
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,737
Reputation: 583
Default Re: How to create randoms with exceptions

Quote:
Originally Posted by coool View Post
OK, But will this work or not??
That would work totally fine, just theoretically not as efficient (it can in very rare circumstance produce long loops, since the random inside while could return the same value over and over again (just theoretically!)).
NaS is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Engine exceptions Johny32 Scripting Help 5 02/06/2016 12:07 AM
Crash because of exceptions stormchaser206 Client Support 4 01/10/2013 02:05 AM
Weird exceptions. 2KY Client Support 4 26/09/2012 07:33 PM
IP exceptions from range HeLiOn_PrImE Server Support 5 24/01/2011 04:54 PM
Random Exceptions? (.Aztec); Client Support 0 28/10/2009 06:39 PM


All times are GMT. The time now is 02:18 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.