[General] Generating different random numbers for every player

Level 5
Joined
Jul 31, 2011
Messages
103
Hi HIVE!.

This is my question:

How can i generate different random numbers for every player in the game.

I have the Math random number action, but i dont know how can i check if the first number and second are different.

Can you help me?.

I have a solution but i cant do it in warcraft something like.

Create temporal number = 100
Create Random number (1 - 40)
Is random == temporal ?
YES = random number again.
NO = set temporal = random.
 
Level 11
Joined
Jun 2, 2004
Messages
849
There are some fancy math tricks to get unique random numbers but the easiest is to just store all used values and reroll if you happen to get an already used one. Pretty much what you said there, yeah.

Anyway if you don't know how to store temporary variables, the easiest way for a new coder is to just use globals. Press ctrl-B in the trigger editor, or press the X button on the menu bar. Make an integer (or better yet, integer array) that's entirely intended for use as temporary storage and call it TempInt or something. Then in your trigger, use a loop and store your results in your temporary variable to check new numbers against.
 

Dr Super Good

Spell Reviewer
Level 60
Joined
Jan 18, 2005
Messages
26,722
I assume you want an unique random number per player and not a different random number generator per player.

If the set of numbers to generate is small, track them in a list. Get random element from list and remove it. Repeat for each player. Complexity is O(1) but only works on small number ranges.

For bigger number ranges you can use a hash table to tack which numbers have been issued. If a number has been issued you generate another random number. With big ranges the chance of generating a user number multiple times in a row is very small so 16 odd attempts should as good as always guarantee you a unique random number per player. However in case you get than 1 in a universe case of it generating an infinite sequence of used numbers you will want some default, non-random algorithm just to prevent an infinite loop or thread crash.
 
Level 16
Joined
May 2, 2011
Messages
1,320
ummm,

why is it not possible to just do it in a For loop, from 0 to 11 or something? and then having Radom being an array?

do you know what is an array? it is basically
a variable that has an index, so if I want gold for all players I'd do something like:
For A = 0 to 11 Do
Set Gold[A] = Gold of player A

so Gold0 will register value of player 1 etc gold11 will register for last player
 
Top