~ FN_Linkin Park
Forum




Math.Random
15 replies



~ FN_Linkin Park
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

A very good parameter for math.randomseed() is os.clock.
So you should use:
1
2
2
math.randomseed(os.clock) math.random()
1
2
3
4
5
6
7
8
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.
================================================================

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.
1
2
3
4
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..
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"
I hope you see the problems now:





fixed version:
1
parse("explosion "..math.random(1,100).." "..math.random(1,100).." 10 200 "..killer)
You're wrong, i've did this, and the sequences are others

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

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
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.



