Typocrite
|
  |
| Joined: 28 Aug 2013 |
| Total Posts: 6856 |
|
|
| 11 Jun 2014 12:36 PM |
Let's say you want to randomize a value between a set of numbers, but you want to exclude only 1 or 2 numbers?
Say x = math.random(1,99)
What I really want to do is exclude a variable from the range. Is that possible? My idea is: x = math.random(1,99 - VARIABLE)
But I doubt that will work.. so is this possible? |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 11 Jun 2014 12:36 PM |
| Why wouldn't it be possible? |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 11 Jun 2014 12:38 PM |
math.random(1, math.random(99, 120)) Is that kinda what you where looking for |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:39 PM |
you can do
while x == variable do x = math.random(1, 99) end |
|
|
| Report Abuse |
|
|
Typocrite
|
  |
| Joined: 28 Aug 2013 |
| Total Posts: 6856 |
|
|
| 11 Jun 2014 12:41 PM |
Well here is the code I've come up with:
print("RescueTower1") _G["G_BLDG_RESCUETOWER1_X"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_X"]) _G["G_BLDG_RESCUETOWER1_Y"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_Y"])
print("RescueTower2") _G["G_BLDG_RESCUETOWER2_X"] = math.random(1,99 - _G["G_BLDG_RESCUETOWER1_X"] ) print(_G["G_BLDG_RESCUETOWER2_X"]) _G["G_BLDG_RESCUETOWER2_Y"] = math.random(1,99 - _G["G_BLDG_RESCUETOWER1_Y"] ) print(_G["G_BLDG_RESCUETOWER2_Y"])
print("RescueTower3") _G["G_BLDG_RESCUETOWER3_X"] = math.random(1,99 - _G["G_BLDG_RESCUETOWER2_X"] ) print(_G["G_BLDG_RESCUETOWER3_X"]) _G["G_BLDG_RESCUETOWER3_Y"] = math.random(1,99 - _G["G_BLDG_RESCUETOWER2_Y"] ) print(_G["G_BLDG_RESCUETOWER3_Y"])
What I am trying to do here is prevent a tower from spawning in the same place as another
So far it's working, testing repeatedly to see if any tower will spawn in the same place |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:41 PM |
local x = math.random(1,99)
X doesn't exist until you put local in front of it. It kinda creates the variable. You can't say x = script.Parent You have to say local x = script.Parent |
|
|
| Report Abuse |
|
|
| |
|
|
| 11 Jun 2014 12:43 PM |
| Is _G["G_BLDG_RESCUETOWER3_X"] a object or a new variable. |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 11 Jun 2014 12:43 PM |
| @Dancing, you are so wrong I don't even want to go there. You can put x=script.Parent. Don't listen to that horrendous advice. |
|
|
| Report Abuse |
|
|
| |
|
Typocrite
|
  |
| Joined: 28 Aug 2013 |
| Total Posts: 6856 |
|
|
| 11 Jun 2014 12:44 PM |
| It's a global variable and my code seems to be working so you can disregard this thread |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:45 PM |
| Are you trying to tell me that I mess up on local x = script.Parent because it still works. |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:45 PM |
| I guess we are done. MISSION ACCOMPLISHED |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:46 PM |
You do >not< need to put local in front of a variable local if for scopes n such |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:46 PM |
| If the code was always working, why did you post it? |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:46 PM |
| Some people like putting their local's. |
|
|
| Report Abuse |
|
|
Typocrite
|
  |
| Joined: 28 Aug 2013 |
| Total Posts: 6856 |
|
|
| 11 Jun 2014 12:47 PM |
ACTUALLY, DON'T DISREGARD THE THREAD
Here's the output
RescueTower1 71 56 RescueTower2 14 42 RescueTower3 8 1
My code is not creating exceptions, it is just subtracting the previous coordinates from the next one, making each successive coordinate lower.
So actually my code does not work because it doesn't allow the towers to spawn just anywhere- got any ideas? |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 11 Jun 2014 12:48 PM |
| but what you said about you needing local is completely inaccurate. |
|
|
| Report Abuse |
|
|
robomax11
|
  |
| Joined: 07 Jul 2011 |
| Total Posts: 6828 |
|
|
| 11 Jun 2014 12:51 PM |
@the stupid person who deosnt know what local is
function showwhatlocal() local p = 9 end
print(p)
tell me what the output is, and what local means
siggys r stupid |
|
|
| Report Abuse |
|
|
Typocrite
|
  |
| Joined: 28 Aug 2013 |
| Total Posts: 6856 |
|
|
| 11 Jun 2014 12:54 PM |
Ok I've changed something but now it broke it
I changed the operator to ~= instead of - so it is "not equal to" instead of subtracting but as it turns out, that is a boolean instead of an operator so that is why the script is broken
12:52:21.163 - Players.Player1.PlayerGui.ScreenGui.Intro:76: bad argument #2 to 'random' (number expected, got boolean)
print("RescueTower1") _G["G_BLDG_RESCUETOWER1_X"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_X"]) _G["G_BLDG_RESCUETOWER1_Y"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_Y"])
print("RescueTower2") _G["G_BLDG_RESCUETOWER2_X"] = math.random(1,99 ~= _G["G_BLDG_RESCUETOWER2_X"]) print(_G["G_BLDG_RESCUETOWER2_X"]) _G["G_BLDG_RESCUETOWER2_Y"] = math.random(1,99 ~= _G["G_BLDG_RESCUETOWER1_Y"] ) print(_G["G_BLDG_RESCUETOWER2_Y"])
Going to try something else, tell me if you got any ideas |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 12:58 PM |
What I would do is have a table of coordinates that are available. When you use up the coordinate, remove it from the table and add it to another one (for future reference, if ever needed).
local Open = { -- Available {X = 1, Y = 1}, {X = 1, Y = 2}, {X = 2, Y = 1}, {X = 2, Y = 2} }
local Closed = {} -- Unavailable
function GetRandomCoordinate() local r = math.random(1, #Open) local coord = Open[r] table.remove(Open, r) table.insert(Closed, coord) return coord end
local c = GetRandomCoordinate() print( c.X, c.Y ) |
|
|
| Report Abuse |
|
|
L0cky2013
|
  |
| Joined: 30 Jul 2012 |
| Total Posts: 1446 |
|
|
| 11 Jun 2014 01:00 PM |
@Agent, I would have done that two.
@OP, this might suit you better
print("RescueTower1") _G["G_BLDG_RESCUETOWER1_X"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_X"]) _G["G_BLDG_RESCUETOWER1_Y"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_Y"])
print("RescueTower2") _G["G_BLDG_RESCUETOWER2_X"] = math.random(1,99/_G["G_BLDG_RESCUETOWER1_X"] +99%_G["G_BLDG_RESCUETOWER1_X"]) print(_G["G_BLDG_RESCUETOWER2_X"]) _G["G_BLDG_RESCUETOWER2_Y"] = math.random(1,99/_G["G_BLDG_RESCUETOWER1_Y"]+99%_G["G_BLDG_RESCUETOWER1_Y"]) print(_G["G_BLDG_RESCUETOWER2_Y"])
print("RescueTower3") _G["G_BLDG_RESCUETOWER3_X"] = math.random(1,99/_G["G_BLDG_RESCUETOWER2_X"] +99%_G["G_BLDG_RESCUETOWER2_X"] ) print(_G["G_BLDG_RESCUETOWER3_X"]) _G["G_BLDG_RESCUETOWER3_Y"] = math.random(1,99/_G["G_BLDG_RESCUETOWER2_Y"] +99%_G["G_BLDG_RESCUETOWER2_Y"]) print(_G["G_BLDG_RESCUETOWER3_Y"]) |
|
|
| Report Abuse |
|
|
Typocrite
|
  |
| Joined: 28 Aug 2013 |
| Total Posts: 6856 |
|
|
| 11 Jun 2014 01:04 PM |
| The table idea is great! The only problem is it will be a fairly large table (99 values for each coordinate) but it is the best option I have- thanks a million |
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 01:04 PM |
_G["G_BLDG_RESCUETOWER1_Y"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER1_Y"])
print("RescueTower2") _G["G_BLDG_RESCUETOWER2_X"] = math.random(1,99) print(_G["G_BLDG_RESCUETOWER2_X"])
if _G["G_BLDG_RESCUETOWER2_X"] == _G["G_BLDG_RESCUETOWER1_Y"] then return _G["G_BLDG_RESCUETOWER2_X"]
I don't think I used the return function correctly but you understand what I am trying to say.
|
|
|
| Report Abuse |
|
|
|
| 11 Jun 2014 01:06 PM |
"The only problem is it will be a fairly large table (99 values for each coordinate) but it is the best option I have- thanks a million"
You can use a loop to generate the coordinates as well. What kind of values will each coordinate have? |
|
|
| Report Abuse |
|
|