Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to select a NEW random number?

So, im making this minigame type of game, and i need all the maps to be different. I tried using a loop to find a new random value, but it makes the game laggy and does not seem very efficient. I found something called Random.new() on the devforum, but have no idea how to use it. here's what i tried so far.

	local maps = game.ReplicatedStorage.Maps:GetChildren()
	local map1 = Random.new(1,#maps)
	local map2 = Random.new(1,#maps)
	local map3 = Random.new(1,#maps)
	script.Map1.Value = map1.Name
	script.Map2.Value = map2.Name
	script.Map3.Value = map3.Name

3 answers

✓ Accepted answer

Hey, try this and see if it works fine :)

local maps = game.ReplicatedStorage.Maps:GetChildren()

for i = 1, 3, 1 do
	script["Map"..i].Value = math.random(1, #maps)
end

Additionally, if you want the maps that have been chosen already to not appear again, you will need to use tables.

here's how I'd do it.

local maps = game.ReplicatedStorage.Maps:GetChildren()


for i = 1, 3, 1 do
	map = math.random(1,#maps)
	script["Map"..i].Value = map
 	table.remove(maps, map)
end

This will remove the map that has already been chosen so the next one that gets selected will not be the same.

9 comments

0

before editing my answer, I recommend looking at the code context, Feahren. :)

0

#maps defines the amount of time the loop iterates based on the amount of children. This way you don’t have to change 3 to how many new maps you add. With additive iteration as well, only two values need to be defined, I suggest writing  1,#maps

0

you got discord?

0

I need to talk to you since it's pretty clear you don't understand what the issue is, it's easier to talk in there than here

0

if not, I'll just say it all here. He is selecting specifically 3 random maps, not all of them. he needs only 3 values, not the number of maps that can be randomly chosen, think of it as having many things to choose from but you only need 3 of them, a full loop of all the available things would not work.

0

also you had #maps, 1 which would error even if the context was correct because you did not add -1 as the third parameter of the loop so it would not start at all, consider removing the downvote cos what you did was completely wrong.

1

@Feahren It is the NUMBER OF TIMES the loop will iterate, not the AMOUNT OF TIME it will take.

1

no one care about my more indepth answer? :(

0

Right, I understand, my bad. I am aware of the semantic error I would’ve caused as well. I clearly resolved that in my previous comment

That my friend is not how the Random class works. Let me explain.

The Random class allows you to create a new random object and you can also specify an optional seed. Such that: rand Random.new([string | float Seed]) using this newly created random object you have the choice of 2 functions one is: int rand:NextInteger(int Min, int Max) which returns a random integer between min and max. The other function you have is: float rand:NextNumber([float Min = 0, float Max = 1]) this returns a random float between min and max which will be 0 to 1 if not specified. The random object also has another function rand rand:Clone() which returns a new random object with the same seed and internal iteration.

So here is how you would use it:

local rand = Random.new()

local randomInteger = rand:NextInteger(0, 10)

print(randomInteger) -- Output: 1, 2, ... 10

And I know this isn't a request site, but for your case you would do this:

local maps = game.ReplicatedStorage.Maps:GetChildren()
local rand = Random.new()

-- You also need to index the "maps" table in order to get the objects, otherwise you would just get a number between 1 and the number of maps
local map1 = maps[rand:NextInteger(1, #maps)]
local map2 = maps[rand:NextInteger(1, #maps)]
local map3 = maps[rand:NextInteger(1, #maps)]
script.Map1.Value = map1.Name
script.Map2.Value = map2.Name
script.Map3.Value = map3.Name

The above code can be simplified to:

local maps = game.ReplicatedStorage.Maps:GetChildren()
local rand = Random.new()

for i = 1, 3 do
	local map = maps[rand:NextInteger(1, #maps)]
	local value = script:FindFirstChild("Map"..i)
	if value then
		value.Value = map.Name
	end
end

I hope this helps!

1 comment

0

Great answer, although i prefer something more simple. wish i could accept two.

Can't you set a variable to a random integer.

local map = math.random(1,10)
If map == 1 then
Elseif map == 2

ECT.

This discussion is preserved as it was. New answers, comments, and votes are closed.