Forum

> > CS2D > Scripts > Math.Random
Forums overviewCS2D overview Scripts overviewLog in to reply

English Math.Random

15 replies
To the start Previous 1 Next To the start

old Math.Random

J4x
User Off Offline

Quote
Hi, i have a doubt here, I have seen that in lua we use math.random, i know where to use it but i don't know how, can someone explain me?

~ FN_Linkin Park

old Re: Math.Random

DC
Admin Off Offline

Quote
you use it whenever you need to generate random numbers.

old Re: Math.Random

TimeQuesT
User Off Offline

Quote
examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function rand()
yeah(math.random(1,10))
end

function yeah(number)
msg (number)
end

--or

a = math.random(1,10)

--or

if (math.random(1,10)==10) then
msg ("yeah it's 10!")
else
msg ("it's not 10! :(")
end

old Re: Math.Random

ohaz
User Off Offline

Quote
always use math.randomseed() before using math.random (or it will always generate the same numbers).
A very good parameter for math.randomseed() is os.clock.
So you should use:
1
2
math.randomseed(os.clock)
math.random()

old Re: Math.Random

DannyDeth
User Off Offline

Quote
math.random is Lua's random number generator ( supplied in the math module ). It is used to make random numbers. As TKD math.randomseed is the 'seed' ( hence the name ) that controls the random pattern.

old Re: Math.Random

DragonAwper
User Off Offline

Quote
Showing the first TimeQuesT Example

1
2
3
4
5
6
7
8
addhook("second","rndnumber")
function rndnumber()
show_numbs(math.random(1,10))
end

function show_numbs(number)
msg ("©000000255"..number.."@C")
end

••••••••••••••••••••
when you start the game, will appear one number per second
in your screen.
••••••••••••••••••••

================================================================
@TheKilledDeath:
i tested my script and aways when i restart the game, the random numbers are others.
================================================================

old Re: Math.Random

Banaan
User Off Offline

Quote
Probably because math.random automatically gets seeded on initialisation. Without restarting, however, it'll output the same.

old Re: Math.Random

Lee
Moderator Off Offline

Quote
math.random doesn't seed automatically. On vanilla distributions, the sequence will always generate:

1,5,2,9,6,5,4,9,...

Possible abuse case:

Random weapon box unseeded, then early on in the game, one can strategically place himself at some predefined position in order to receive the weapon of choice.

Quote
i tested my script and aways when i restart the game, the random numbers are others.


CS2D only loads the lua engine once per run. Close your server, then open it up again, and check out the sequence of numbers that are generated. I guarantee you that they are the same sequence.

old Re: Math.Random

J4x
User Off Offline

Quote
Thanks, Is this correct?
1
2
3
4
parse("explosion "..math.random(1,100)).." "..math.random(1,100)).." '10,200,killer")
parse("explosion "..math.random(1,100)).." "..math.random(1,100)).." '10,200,killer")
parse("explosion "..math.random(1,100)).." "..math.random(1,100)).." '10,200,killer")
parse("explosion "..math.random(1,100)).." "..math.random(1,100)).." '10,200,killer")

PS: this just the part of a code..

old Re: Math.Random

DC
Admin Off Offline

Quote
no, this is not correct. and it looks like you pasted this together without really understanding it 100%. you should read a Lua tutorial.

first of all: math.random(1,100))
one pointless ). remove it.

now lets see what the concatenation with ".." creates (you should always play through this when you concatenate strings):
1
"explosion x y '10,200,killer"
(with x and y being random numbers)
I hope you see the problems now:
• why the ' before 10? it's wrong
• why are you starting to use , to separate parameters after 10? you did it right before the 10! cs2d cmd explosion is a cs2d command, cs2d command parameters are separated by spaces!
• what about the last parameter? it is a string. "killer". cs2d cmd explosion expects a player ID (a number)! maybe you wanted to use the variable killer? then you have to concatenate it with ".." like the random stuff

fixed version:
1
parse("explosion "..math.random(1,100).." "..math.random(1,100).." 10 200 "..killer)
(assuming that killer is a Lua variable which contains the player id of the killer. you could completely remove the last parameter if the explosion is not caused by any player)

old Re: Math.Random

Flacko
User Off Offline

Quote
@DragonAwper

You must be doing it wrong because the sequences are the same (just tested)

old Re: Math.Random

Lee
Moderator Off Offline

Quote
@DragonAwper:

Close the CS2D server completely, and then restart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
static int math_random (lua_State *L) {
	/* the `%' avoids the (rare) case of r==1, and is needed also because on
		 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
	lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
	switch (lua_gettop(L)) {	/* check number of arguments */
		case 0: {	/* no arguments */
			lua_pushnumber(L, r);	/* Number between 0 and 1 */
			break;
		}
		case 1: {	/* only upper limit */
			int u = luaL_checkint(L, 1);
			luaL_argcheck(L, 1<=u, 1, "interval is empty");
			lua_pushnumber(L, floor(r*u)+1);	/* int between 1 and `u' */
			break;
		}	
		case 2: {	/* lower and upper limits */
			int l = luaL_checkint(L, 1);
			int u = luaL_checkint(L, 2);
			luaL_argcheck(L, l<=u, 2, "interval is empty");
			lua_pushnumber(L, floor(r*(u-l+1))+l);	/* int between `l' and `u' */
			break;
		}
		default: return luaL_error(L, "wrong number of arguments");
	}
	return 1;
}


static int math_randomseed (lua_State *L) {
	srand(luaL_checkint(L, 1));
	return 0;
}

math_randomseed is only referenced once in the entire source code of Lua, which means random is seeded with the default value, 1.

C Reference for srand has written
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview