Scripts / random number

Author
Message
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 21st Feb 2014 13:04
is there a way to generate a random number in FPSC-R yet?

i've tried

Quote: "

RND(1,100)

random(1,100)

math.random(1,100)

rnd(1,100)

"


none of which seem to work for me, either just outright crashing FPSC or giving me an undeclared variable error

i know i've seen it posted in a code snippet from someone before but i can't find it

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
AuShadow
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 2nd Dec 2013
Location: Australia
Posted: 21st Feb 2014 13:13 Edited at: 21st Feb 2014 14:28
yeah i got it working in my cash script here:



its math.random(10,50) gives a random number between 10 and 50, however i have found that the "random numbers" seem to always be the same for me, by which i mean if i run a test game with 10 pickups, each pickup is different for that run but then if i run again it will be the same as the last run, all 10 pickups = same amount as first run if that makes sense, i use it in a variable however so for example variable called number would be as follows



-- declare variable

number = 0



-- make random number for variable value

number = math.random(1,100)



-- in the code snippet below i have it adding to a variable in this case current cash which here i will call money for ease of reading so...

money = money + math.random(1,100);



that should generate a random number and then assign it to number.

hope this helps, i use it in beta 1.0045 and works







oh and if you check the wiki under scripts help i posted a big section on all the math. functions



hopefully I have helped you after all the help you've given me

PC Specs: Windows 7 home 64-bit, Amd 7900 3gb DDR5 graphics, 8gb DDR3 Ram, Intel i7 3.4ghz

Feel free to visit and edit the public FPSCR resource wiki page: http://fpscrresource.wikispaces.com/home
AuShadow
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 2nd Dec 2013
Location: Australia
Posted: 21st Feb 2014 14:47
Update if you go to here:

http://www.lua.org/cgi-bin/demo



and punch in: print(math.random(1,5000))

and then run

you get a random outputted number, in my case this was 4201, but then

if i go restart punch in the same code and run again i get the same

result.



then i entered this:



print(math.random(1,5000));

print(math.random(1,5000));

print(math.random(1,5000));

print(math.random(1,5000));

print(math.random(1,5000));

print(math.random(1,5000));

print(math.random(1,5000));

print(math.random(1,5000));



and results were as such:



4201

1972

3916

3993

4559

988

1677

3842



i then cleared the code and re-entered it and ran again and.......

the exact same result, i then changed the math.random(1,5000) to

math.random(1,5001) a big change i know but with this change the results were:



only some changed and even then only by adding 1 to the number,



so im not sure why but math.random in lua is actually not very random at all,

it seems to run on a set equation and changes the number based on how many times

math.random had already been called, Anyone got any suggestions for this?

PC Specs: Windows 7 home 64-bit, Amd 7900 3gb DDR5 graphics, 8gb DDR3 Ram, Intel i7 3.4ghz

Feel free to visit and edit the public FPSCR resource wiki page: http://fpscrresource.wikispaces.com/home
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 21st Feb 2014 14:54
thanks that at least doesnt crash out now but it's not random at all from what i can see



if i set it to
Quote: "math.random(10,50)"
or any other numbers it will always return the first number (in this case 10) no matter what..

do u always get an extra $100 when u run your code snippet from above or is it really random amounts for u?







and yes i noticed that too, values from previous loads are stored and carry over to reloads... i.e. loading a map again after having spent some money or something will keep the previous amount... i assume this is because scripts are not currently reloaded each time the game is launched and have no idea if the same issue actually occurs in a standalone or not.

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
AuShadow
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 2nd Dec 2013
Location: Australia
Posted: 21st Feb 2014 14:59
if you see my previous post it seems to always run the same sequence of numbers , however it will only run each number once so your next pickup would be a different number each time but each run though would be identical to the last if you get what i mean

PC Specs: Windows 7 home 64-bit, Amd 7900 3gb DDR5 graphics, 8gb DDR3 Ram, Intel i7 3.4ghz

Feel free to visit and edit the public FPSCR resource wiki page: http://fpscrresource.wikispaces.com/home
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 21st Feb 2014 18:19 Edited at: 21st Feb 2014 19:15
nevermind it's just started working as it should... seems i forgot i had added a
Quote: "math.randomseed()"
line which was causing it to not be random



time to see how many more errors i can get XD



edit: thanks AuShadow, working perfectly now

here's what i was trying to achieve - a kind of random code dispenser i guess (could be used for numeric locks etc with some tweaking - and a command for player input ofcourse )







video here

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11

Login to post a reply

Server time is: 2024-12-22 01:16:37
Your offset time is: 2024-12-22 01:16:37