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

How do you make math.random more random?

I made a Music Script that chooses random songs from the table and plays them, some songs are played and then get replayed again. How do you make it more random?

--Scripted by iiHeroAviator
local MusicList = {'319758951','310948899','272156573','254913653','320512903','338431940','359207189','142762345','243372213','377436683','298199818'} --Add Music IDs here.

--DO NOT EDIT BELOW IF YOU DONT UNDERSTAND THE SCRIPT BELOW.--

local mr = math.random
local sound = workspace:WaitForChild('InGameMusic')
while true do
	sound:Stop()
	local randomSong = MusicList[mr(1, #MusicList)]
	local msg = workspace.InGameMusic:WaitForChild('SongPlaying')
	local msg2 = script.NewSongIn
	wait(.5)
	sound.SoundId = 'rbxassetid://'..randomSong
	sound:Play()
	msg.Value = 'Now Playing: '..game:GetService('MarketplaceService'):GetProductInfo(randomSong).Name..' ('..randomSong..')'
	print('[MUSIC SCRIPT] Now Playing: '..randomSong..'.')
	for i = 5,0,-1 do
		msg2.Value = 'Choosing song in: '..math.floor(i/60) .. ":" .. ("0"..(i % 60)):sub(-2)
		wait(1)
	end
end

5 comments

0

What do you mean by "more random"? If you're picking songs truly randomly, they will get re-played twice in a row fairly often.

0

@ScriptGuider wow sneaky self advertisement

0

By "more random" I mean like.... I don't want the same thing to be chosen constantly, I want ALL the songs to be played without it replaying again. And after they are all played, then it will play them all over again.

3 answers

You don't want it to be "more random", you want this:

I want ALL the songs to be played without it replaying again. And after they are all played, then it will play them all over again

Let's tease this into something more precise:

  • While there are uplayed songs,
    • play a (random) unplayed song
-- here's just the "sketch". I just use ":Play()" to mean "play the song with this ID"

while true do
	-- Re-fill the list of unplayed songs
	local unplayed = {
		-- whatever songs
	}
	
	while #unplayed > 0 do
		-- Remove a random song from the list of unplayed songs
		local randomSong = table.remove(unplayed, math.random(#unplayed))

		-- Play that song
		randomSong:Play()
	end
end

The thing about random being random, is that it's RANDOM. To fix consecutive tunes store the old music in a variable, if it's the same as the latest music, then remix the music and keep trying till it's not the same music.

#What you can do? Use randomseed which determines what "seed" it uses to get random numbers.


#math.randomseed() This determines the seed that the random numbers would be chosen by. For example how Minecraft seeds make maps through the seed and will always get different maps that follow the rules of the seed.

math.randomseed(12376) --that is the seed
print(math.random(100)) --prints a random number 1-100 that was derived from set seed.

This isn't exactly random. The first time you run it you will always get the same number, it's impossible to get an exactly random number. But we can use outside influences such as the time or number of characters in each player's name added together for example:

math.randomseed(tick())
print(math.random(100)) --Prints a number 1-100 and the seed is the amount of seconds since January 1, 1970. Pretty much "random".

#Final Product

--Scripted by iiHeroAviator
local MusicList = {'319758951','310948899','272156573','254913653','320512903','338431940','359207189','142762345','243372213','377436683','298199818'} --Add Music IDs here.

--DO NOT EDIT BELOW IF YOU DONT UNDERSTAND THE SCRIPT BELOW.--

math.randomseed(tick())
local sound = workspace:WaitForChild('InGameMusic')
while true do
    sound:Stop()
    local randomSong = MusicList[math.random(#MusicList)]
    local msg = workspace.InGameMusic:WaitForChild('SongPlaying')
    local msg2 = script.NewSongIn
    wait(.5)
    sound.SoundId = 'rbxassetid://'..randomSong
    sound:Play()
    msg.Value = 'Now Playing: '..game:GetService('MarketplaceService'):GetProductInfo(randomSong).Name..' ('..randomSong..')'
    print('[MUSIC SCRIPT] Now Playing: '..randomSong..'.')
    for i = 5,0,-1 do
        msg2.Value = 'Choosing song in: '..math.floor(i/60) .. ":" .. ("0"..(i % 60)):sub(-2)
        wait(1)
    end
end


Hope it helps!

2 comments

0

It is working, but this ID (377436683) gets chosen VERY, VERY, constantly.

0

Randomseed is not the problem here... Nor should you seed it plainly with tick()

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