SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 02/10/2019, 08:04 PM   #1
GospodinX
Big Clucker
 
Join Date: Nov 2017
Posts: 148
Reputation: 20
Default Six random numbers which are not equal to each other

Hi

I need to make 6 ( 0 - 45) random numbers which are not equal to each other.I have this

Code:
	Number[0] = random(45);
	Number[1] = random(45);
	Number[2] = random(45);
	Number[3] = random(45);
	Number[4] = random(45);
	Number[5] = random(45);
I really don't have idea how to do it..I can only something like this(but i think that it's bad idea)

Code:
if(Number[1] == Number[2]) Number[2] = random(45);
GospodinX is offline   Reply With Quote
Old 03/10/2019, 12:53 AM   #2
CheezIt
Big Clucker
 
CheezIt's Avatar
 
Join Date: Apr 2017
Posts: 178
Reputation: 17
Default Re: Six random numbers which are not equal to each other

You can create an array that goes from 0-45, swap the value of the selected index with the last index and decrease the size of the array by 1. Ezpzlmnsqz
__________________
I'd like to bring back my old CNR server that was created from scratch & hosted years ago. I'll host & update everything (including the website & forum). You would pay for hosted tab and help manage the community. PM me if you're interested!
CheezIt is offline   Reply With Quote
Old 03/10/2019, 09:10 AM   #3
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Six random numbers which are not equal to each other

With YSI 5:
pawn Code:
new Iterator: rand_number<46>, counter;

for (new i = 0; i < 6; i++)
{
    Iter_RandomAdd(rand_number); // adds 6 random numbers that are not in iterator
}

foreach(new i : rand_number) // loop through the 6 random numbers
{
    Number[counter++] = i; // assign them to `Number` array
}

Another way is to assign each value in an array, randomly shuffle it and choose the 6 first or last elements.

Last edited by Calisthenics; 03/10/2019 at 11:07 AM.
Calisthenics is offline   Reply With Quote
Old 03/10/2019, 10:45 AM   #4
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,712
Reputation: 1671
Default Re: Six random numbers which are not equal to each other

Code:
new Number[6];

for (new i = 0; i < 6; i++)
{
     Number[i] = random(45);
     if (i == 0) continue;
     if (Number[i] == Number[i-1]) i--; //Decrease i so that current Number[i] gets re-assigned untill it's different
}
There are multiple ways to do it off course. I would use this way (in case you don't use YSI. I don't so that's why I'd run it like this).
It runs this loop untill all numbers are different
Kwarde is offline   Reply With Quote
Old 03/10/2019, 11:11 AM   #5
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Six random numbers which are not equal to each other

The other way I mentioned:
pawn Code:
new dump_arr[46];

for (new i = 0; i < 46; i++)
{
    dump_arr[i] = i; // assign value from 0 to 45 in array
}

shuffle(dump_arr); // randomly shuffle it

for (new i = 0; i < 6; i++)
{
    Number[i] = dump_arr[i]; // assign 6 first random numbers
}
pawn Code:
shuffle(arr[], size = sizeof (arr))
{
    new r,
        temp;

    for (new i, j = size - 1; i < j; i++)
    {
        r = i + random(size - i);
        temp = arr[i];

        arr[i] = arr[r];
        arr[r] = temp;
    }
}

Quote:
Originally Posted by Kwarde View Post
Code:
new Number[6];

for (new i = 0; i < 6; i++)
{
     Number[i] = random(45);
     if (i == 0) continue;
     if (Number[i] == Number[i-1]) i--; //Decrease i so that current Number[i] gets re-assigned untill it's different
}
There are multiple ways to do it off course. I would use this way (in case you don't use YSI. I don't so that's why I'd run it like this).
It runs this loop untill all numbers are different
It cannot guarantee there will be no duplicates as you only compare current with previous index.
Calisthenics is offline   Reply With Quote
Old 03/10/2019, 11:17 AM   #6
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 272
Reputation: 27
Default Re: Six random numbers which are not equal to each other

Quote:
Originally Posted by Kwarde View Post
Code:
new Number[6];

for (new i = 0; i < 6; i++)
{
     Number[i] = random(45);
     if (i == 0) continue;
     if (Number[i] == Number[i-1]) i--; //Decrease i so that current Number[i] gets re-assigned untill it's different
}
There are multiple ways to do it off course. I would use this way (in case you don't use YSI. I don't so that's why I'd run it like this).
It runs this loop untill all numbers are different
This will only keep Number[i] and Number[i-1] different.
Meaning Number[0] and Number[1] can't be the same, but Number[2] can be the same as Number[0]
Change random(45) to random(6), print values of Number and you see what I mean:
Code:
Number 0 = 4
Number 1 = 1
Number 2 = 5
Number 3 = 2
Number 4 = 1
Number 5 = 4
Plastikmensch is offline   Reply With Quote
Old 03/10/2019, 11:19 AM   #7
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,712
Reputation: 1671
Default Re: Six random numbers which are not equal to each other

I got it right after the first sentance and you are so right. I think I need more coffee /me is being slightly ashamed
Kwarde is offline   Reply With Quote
Old 03/10/2019, 11:32 AM   #8
BroZeus
High-roller
 
BroZeus's Avatar
 
Join Date: Jan 2014
Location: India
Posts: 1,398
Reputation: 173
Default Re: Six random numbers which are not equal to each other

Another way to do it is to pick random numbers from consecutive ranges, for example, pick the first random number from the range 0-9, second from 10-19 and so on.
Of course, it won't have that level of randomness as picking it from the same range over and over again, but yea, just wanted to present a different outlook.
__________________


"Be the change that you wish to see in the world."

Check out my Github profile!
BroZeus is offline   Reply With Quote
Old 03/10/2019, 07:58 PM   #9
GospodinX
Big Clucker
 
Join Date: Nov 2017
Posts: 148
Reputation: 20
Thumbs up Re: Six random numbers which are not equal to each other

Quote:
Originally Posted by Calisthenics View Post
With YSI 5:
pawn Code:
new Iterator: rand_number<46>, counter;

for (new i = 0; i < 6; i++)
{
    Iter_RandomAdd(rand_number); // adds 6 random numbers that are not in iterator
}

foreach(new i : rand_number) // loop through the 6 random numbers
{
    Number[counter++] = i; // assign them to `Number` array
}

Another way is to assign each value in an array, randomly shuffle it and choose the 6 first or last elements.
I use YSI 5..Thank you,I will try this
GospodinX 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
Help with random numbers ,TomY' Scripting Help 2 17/07/2018 11:53 AM
Random Numbers TuTh Scripting Help 1 04/11/2012 08:31 AM
Random numbers Jimbo01 Help Archive 2 01/02/2011 07:45 PM
[Help] Random numbers Rainbow_Six Help Archive 4 21/10/2010 06:08 PM
Need help with random numbers... *BueNoOo* Help Archive 11 18/06/2009 04:13 PM


All times are GMT. The time now is 04:52 AM.


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