Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / how to get different numbers out of rnd() even sometimes numbers are even?

Author
Message
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 8th Oct 2010 16:21
what i want to do is:

if i have a map of 10 size, and i will add players random on that 10 size, with rnd() sometimes there will be the same number, how can i avoid this?





are there maybe array options for?
No Time To Code
15
Years of Service
User Offline
Joined: 22nd Dec 2008
Location:
Posted: 8th Oct 2010 16:38
Add Randomize timer() at the start of your programs:



which will reseed the random number generator.

It is not from the benevolence of the butcher, the brewer, or the baker, that we expect our dinner, but from their regard to their own interest. --Adam Smith
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Oct 2010 17:00
Is that what he meant? I assumed he meant that you might get two 4's in a row. Perhaps T4r4ntul4 could clarify? If I'm right he just needs a simple rule for sampling without replacement.
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 8th Oct 2010 17:11
@No Time To Code

with your example i get the output:



what i want is that there are no duplicated numbers, like the 5's in this example

@Green Gandalf

yes thats what i meant, just 4 or more/less random numbers but not the same numbers in a single output.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 8th Oct 2010 18:39 Edited at: 8th Oct 2010 18:40
Quote: "yes thats what i meant, just 4 or more/less random numbers but not the same numbers in a single output."


You can use an array to store a 0 if the number hasn't been picked already or a 1 if it has. Once a number has been picked check the array to see if it hasn't been already used and if so make the array at the numbers spot equal a 1. If the number has been picked already it doesn't add to the array and picks a different random number on the next loop.



T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 8th Oct 2010 19:28
thats nice grog.

but i want it a little more complicated:

say if i had 10 rooms, and i have 4 players, how can i place them randomly in the rooms without having players in the same room?
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 8th Oct 2010 21:24 Edited at: 8th Oct 2010 22:20
Grogs code is good, you just have to adjust it for only 4 players and ten rooms.

Another possible way to do this is to start with an array of 10 elements, each element holding its own index, i.e. Room(1)=1, Room(2)=2, etc.

Then simply mix them up. Pick one element and swap it with another one thousand times, and you'll have a random order. Give the first four rooms to the players.

Here is some code to illustrate this:


You could even start with a string of "1234567890" and randomly take one character at a time, ending up with a string like "5467329108", then just use the first four characters as your rooms.

Here is some code for that:


T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 8th Oct 2010 22:18
thanks rich, thats what i needed

Login to post a reply

Server time is: 2024-11-24 10:35:13
Your offset time is: 2024-11-24 10:35:13