Math.random + table help

I’m trying to set this code up so it displays 1 random image from my table, which holds 8 images ,on screen 1 at a time.
I’m halfway there. I got the images to display randomly but they are stacked on each other. Also I’m trying to set it up so if one image from the table gets displayed it won’t show up twice.

displayMode(FULLSCREEN)
function setup()
      et=ElapsedTime
      count=30     
    tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
            asset.builtin.Planet_Cute.Character_Boy,    
            asset.builtin.Planet_Cute.Character_Horn_Girl,              
            asset.builtin.Planet_Cute.Character_Pink_Girl,
            asset.builtin.Space_Art.UFO,      
            asset.builtin.Planet_Cute.Enemy_Bug,     
            asset.builtin.Planet_Cute.Key,       
            asset.builtin.Planet_Cute.Gem_Green, }   
    bTab={}
    for x=-20,20 do
        for y=-20,20 do
            table.insert(bTab,{x=x,y=y,v=math.random(13)})
        end       
    end
    dx,dy=WIDTH/2,HEIGHT/2
end

function draw()
    background(0)
    if ElapsedTime-et>1 then
        count = count - 1
        et=ElapsedTime
    end   
    text(count,WIDTH/2,HEIGHT/2)  

    if count<=29  then
        for a,b in pairs(bTab) do
            if b.v<7 then
            sprite(tab[b.v],dx+200,dy)        
        else
                if count<=28 then
            sprite(tab[b.v-5],dx,dy+200)    
        end
                if count<=27 then
            sprite(tab[b.v],dx,dy-200)    
        end  
    end
    end 
    end
end

@Jarc You have to be a little more specific with what you want. This shows a random sprite from the table with no duplicates. When all eight are shown, the word done is displayed. Not sure what this will accomplish.

displayMode(FULLSCREEN)
function setup()
    et=ElapsedTime
    count=6  
    a=0   
    tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
            asset.builtin.Planet_Cute.Character_Boy,    
            asset.builtin.Planet_Cute.Character_Horn_Girl,              
            asset.builtin.Planet_Cute.Character_Pink_Girl,
            asset.builtin.Space_Art.UFO,      
            asset.builtin.Planet_Cute.Enemy_Bug,     
            asset.builtin.Planet_Cute.Key,       
            asset.builtin.Planet_Cute.Gem_Green, }      
end

function draw()
    background(0)
    if ElapsedTime-et>1 then
        count = count + 1
        et=ElapsedTime
    end
    if count>4 and #tab>0 then
        count=0
        if a>0 then
            table.remove(tab,a)
        end
        if #tab>0 then
            a=math.random(#tab)
        end
    end
    if #tab>0 then
        text(count,WIDTH/2,HEIGHT/2+100)
        sprite(tab[a],WIDTH/2,HEIGHT/2)
    else
        text("done",WIDTH/2,HEIGHT/2)
    end
end

@dave1707 I want to display them all on the screen, in different positions. Basically I’m going for a shuffling cards effect then the screen deals them 1 by 1. What’s in the table are the cards, math.random is the shuffle. Going to use Elapsed time to deal. In a card deck you don’t get the same card dealt twice, so that’s why I don’t want duplicates.

Here’s a card shuffle program. It just prints the cards. You can add the card images if you want. Try doing a forum search for things. Probably everything you’re looking for has been done and there’s probably an example I’ve written for it.

function setup()
    suit={"Diamonds","Hearts","Spades","Clubs"}
    value={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}
    
    Shuffled=shuffleCards()
    
    for z=1,52 do    -- print the shuffled deck
        s=math.ceil(Shuffled[z]/13)   -- get suit 
        v=Shuffled[z]%13+1    -- get value
        print(value[v].." of "..suit[s])
    end
end

function shuffleCards()
    d={}
    for z=1,52 do
        d[z]=z
    end  
    for z=52,2,-1 do
        r=math.random(z)
        h=d[z]
        d[z]=d[r]
        d[r]=h
    end
    return d    -- return shuffled deck
end

@dave1707 also want them to stay on screen after appearing.

@dave1707 My bad I didn’t see that you responded. I’ll check the code out, Thanks.

Here’s another version.

displayMode(FULLSCREEN)

function setup()
    textMode(CORNER)
    fontSize(30)
    fill(255)
    su={"?","?","?","?"}
    value={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
    Shuffled=shuffleCards()
end

function draw()
    background(227, 183, 114)
    c=0
    for x=1,4 do
        for y=1,13 do
            c=c+1
            s=math.ceil(Shuffled[c]/13)   -- get suit 
            v=Shuffled[c]%13+1    -- get value
            text(value[v].." of  "..su[s],x*200-120,HEIGHT-80-y*50)
        end
    end
end

function shuffleCards()
    d={}
    for z=1,52 do
        d[z]=z
    end  
    for z=52,2,-1 do
        r=math.random(z)
        h=d[z]
        d[z]=d[r]
        d[r]=h
    end
    return d    -- return shuffled deck
end

@dave1707 I shouldn’t have brought up playing cards, bad example on my part. I feel off subject. I want the code to display exactly like this but I want the sprites to be random & no duplicates.

Been trying to incorporate the shuffling program for hours but I feel like it’s tedious.

displayMode(FULLSCREEN)
function setup()
      et=ElapsedTime
      count=30     
    tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
            asset.builtin.Planet_Cute.Character_Boy,    
            asset.builtin.Planet_Cute.Character_Horn_Girl,              
            asset.builtin.Planet_Cute.Character_Pink_Girl,
            asset.builtin.Space_Art.UFO,      
            asset.builtin.Planet_Cute.Enemy_Bug,     
            asset.builtin.Planet_Cute.Key,       
            asset.builtin.Planet_Cute.Gem_Green }   
    bTab={}
    for x=-20,20 do
        for y=-20,20 do
            table.insert(bTab,{x=x,y=y,v=math.random(13)})
        end       
    end
    dx,dy=WIDTH/2,HEIGHT/2
end

function draw()
    background(0)
    if ElapsedTime-et>1 then
        count = count - 1
        et=ElapsedTime
    end   
    text(count,WIDTH/2,HEIGHT/2)  
   
    if count<=29  then
        for a,b in pairs(bTab) do
            if b.v<7 then
            sprite(tab[2],dx+200,dy)        
        else
                if count<=28 then
            sprite(tab[3],dx,dy+200)    
        end
                if count<=27 then
            sprite(tab[1],dx,dy-200)   
                    
                if count<=26 then
            sprite(tab[4],dx-200,dy)   
    end  
    end
    end 
    end
    end
    end

This displays random sprites in a circle. You can have as many sprites as you want. You will have to scale them if you have a lot so the don’t overlap.

displayMode(FULLSCREEN)
function setup()
      et=ElapsedTime
      count=1     
    tab={   asset.builtin.Planet_Cute.Character_Princess_Girl, 
            asset.builtin.Planet_Cute.Character_Boy,    
            asset.builtin.Planet_Cute.Character_Horn_Girl,              
            asset.builtin.Planet_Cute.Character_Pink_Girl,
            asset.builtin.Space_Art.UFO,      
            asset.builtin.Planet_Cute.Enemy_Bug,     
            asset.builtin.Planet_Cute.Key,       
            asset.builtin.Planet_Cute.Gem_Green,
            asset.builtin.Planet_Cute.Gem_Orange,
            asset.builtin.Planet_Cute.Chest_Closed,
            asset.builtin.Planet_Cute.Grass_Block,
            asset.builtin.Planet_Cute.Heart }  
    tab1={} 
    for z=1,#tab do
        a=math.random(#tab)
        table.insert(tab1,tab[a])
        table.remove(tab,a)
    end
    deg=360/#tab1
end

function draw()
    background(0)
    if ElapsedTime-et>2 then
        if count<#tab1 then
            count=count +1
            et=ElapsedTime
        end
    end   
    for z=0,count-1 do
        x=math.cos(math.rad(deg*z))*300
        y=math.sin(math.rad(deg*z))*300
        sprite(tab1[z+1],WIDTH/2+x,HEIGHT/2+y)
    end
end

@dave1707 Perfect! Thanks!

@dave1707 how would I go about giving each sprite a specific x,y coordinate to display at?

I think it’s here but not sure how to set up

 for z=0,count-1 do
        x=math.cos(math.rad(deg*z))*300
        y=math.sin(math.rad(deg*z))*300
        sprite(tab1[z+1],WIDTH/2+x,HEIGHT/2+y)
    end

In the first table, change all the x=200,y=200 to what you want them to be.

displayMode(FULLSCREEN)
function setup()
    et=ElapsedTime
    count=1     
    tab={   {as=asset.builtin.Planet_Cute.Character_Princess_Girl,x=200,y=200},
            {as=asset.builtin.Planet_Cute.Character_Boy,x=200,y=200},    
            {as=asset.builtin.Planet_Cute.Character_Horn_Girl,x=200,y=200},              
            {as=asset.builtin.Planet_Cute.Character_Pink_Girl,x=200,y=200},
            {as=asset.builtin.Space_Art.UFO,x=200,y=200},      
            {as=asset.builtin.Planet_Cute.Enemy_Bug,x=200,y=200},     
            {as=asset.builtin.Planet_Cute.Key,x=200,y=200},       
            {as=asset.builtin.Planet_Cute.Gem_Green,x=200,y=200},
            {as=asset.builtin.Planet_Cute.Gem_Orange,x=200,y=200},
            {as=asset.builtin.Planet_Cute.Chest_Closed,x=200,y=200},
            {as=asset.builtin.Planet_Cute.Grass_Block,x=200,y=200},
            {as=asset.builtin.Planet_Cute.Heart,x=200,y=200} }     
    tab1={} 
    for z=1,#tab do
        a=math.random(#tab)
        table.insert(tab1,tab[a])
        table.remove(tab,a)
    end
end

function draw()
    background(0)
    if ElapsedTime-et>2 then
        if count<#tab1 then
            count=count +1
            et=ElapsedTime
        end
    end   
    for z=1,count do
        sprite(tab1[z].as,WIDTH/2+tab1[z].x,HEIGHT/2+tab1[z].y)
    end
end

@dave1707 It’s not working. Right now the same sprites end up at the same locations every time. I want those specific locations and I want which sprite that shows in those specific locations to be random. To clarify I want a random sprite to appear in a random order in those specific locations every time I run the code.

Example pic below, I get this every time I run code. Pretty much I want everything to be random but the locations.

displayMode(FULLSCREEN)
function setup()
    et=ElapsedTime
    count=1     
    tab={   {as=asset.builtin.Planet_Cute.Character_Princess_Girl,x=200,y=200},
            {as=asset.builtin.Planet_Cute.Character_Boy,x=-200,y=200},    
            {as=asset.builtin.Planet_Cute.Character_Horn_Girl,x=-300,y=200},              
            {as=asset.builtin.Planet_Cute.Character_Pink_Girl,x=-400,y=200},
            {as=asset.builtin.Space_Art.UFO,x=300,y=200},      
        }     
    tab1={} 
    for z=1,#tab do
        a=math.random(#tab)
        table.insert(tab1,tab[a])
        table.remove(tab,a)
    end
end

function draw()
    background(0)
    if ElapsedTime-et>2 then
        if count<#tab1 then
            count=count +1
            et=ElapsedTime
        end
    end   
    for z=1,count do
        sprite(tab1[z].as,WIDTH/2+tab1[z].x,HEIGHT/2+tab1[z].y)
    end
end

Sorry, I was in a hurry this morning and didn’t check what I was doing.

displayMode(FULLSCREEN)

function setup()
    et=ElapsedTime
    count=1
    tab={   asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,x=-200,
            asset.builtin.Planet_Cute.Character_Horn_Girl,
            asset.builtin.Planet_Cute.Character_Pink_Girl,
            asset.builtin.Space_Art.UFO }
    loc={vec2(200,200),vec2(-200,200),vec2(-300,200),vec2(-400,200),vec2(300,200)}
    tab1={}
    for z=1,#tab do
        a=math.random(#tab)
        table.insert(tab1,tab[a])
        table.remove(tab,a)
    end
end

function draw()
    background(0)
    if ElapsedTime-et>2 then
        if count<#tab1 then
            count=count+1
            et=ElapsedTime
        end
    end
    for z=1,count do
        sprite(tab1[z],WIDTH/2+loc[z].x,HEIGHT/2+loc[z].y)
    end
end

@dave1707 Works great! Thanks I appreciate it.

@dave1707 I’m trying to combine the code above with this code, that you previously helped me with.

The code above randomizes sprites then puts in specific locations.
The code below makes the sprites moveable and scale from small circle to big circle.

function setup()
    spriteMode(CENTER)
    ellipseMode(CENTER)
    displayMode(FULLSCREEN)
    tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
    tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
    tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
    tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
            asset.builtin.Planet_Cute.Character_Horn_Girl
        }
    imageSize = spriteSize(tab4[1])
    scaler = .50
end
function draw()
    background(98)
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)
    end
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
    for a,b in pairs(tab3) do
        sprite(tab4[a],b.x,b.y,imageSize*b.z)
    end
end
function touched(touch)    
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end

Posting my progress below

@dave1707 Here is my progress but I think my for, do for the sprite in draw is written wrong. I’ve been trying to make this work for hours.

    function setup()
    spriteMode(CENTER)
    ellipseMode(CENTER)
    displayMode(FULLSCREEN)
    
    et=ElapsedTime
    count=1
     
    tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
    tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
    tab3={vec3(290,-120,.5),vec3(290,-280,.5),vec3(290,190,.5),vec3(290,35,.5)}  -- position of sprite and scaler value
    tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
            asset.builtin.Planet_Cute.Character_Horn_Girl }
    imageSize = spriteSize(tab4[1])
    scaler = .50
    
   tab5={}
    for z=1,#tab4 do
        a=math.random(#tab4)
        table.insert(tab5,tab4[a])
        table.remove(tab4,a)
end   
    
end
function draw()
    background(98)
    strokeWidth(5)
    fill(128)
        for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)
    end
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
    --for a,b in pairs(tab3) do
        --sprite(tab4[a],b.x,b.y,imageSize*b.z)
    --end
        if ElapsedTime-et>.50 then
        if count<#tab5 then
            count=count+1
            et=ElapsedTime
        end
        end
    for z=1,count do
        sprite(tab5[z],WIDTH/2+tab3[z].x,HEIGHT/2+tab3[z].y,imageSize*scaler)
    end
    
end
function touched(touch) 
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end

@Jarc Here’s the other code with random added.

displayMode(FULLSCREEN)    

function setup()
    spriteMode(CENTER)
    ellipseMode(CENTER)

    tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
    tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
    tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
    tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
            asset.builtin.Planet_Cute.Character_Horn_Girl
        }
    imageSize = spriteSize(tab4[1])
    tab5={}
    for z=1,#tab4 do
        a=math.random(#tab4)
        table.insert(tab5,tab4[a])
        table.remove(tab4,a)
    end
    scaler = .50
end

function draw()
    background(98)
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)
    end
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
    for a,b in pairs(tab3) do
        sprite(tab5[a],b.x,b.y,imageSize*b.z)
    end
end

function touched(touch)    
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end

@dave1707 I already had the random added in my code progress. The problem I’m having is making the sprites appear 1 at a time randomly in those specific locations then making the sprites moveable and scale from small circle to big circle.

@dave1707 I have them all appearing after 1sec + random + moveable and scale to circles. All I need to do now is make them appear 1 by 1 every second. Is there a easy way?

displayMode(FULLSCREEN)    

function setup()
    spriteMode(CENTER)
    ellipseMode(CENTER)
    
    et=ElapsedTime
    count=30

    tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
    tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
    tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
    tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
            asset.builtin.Planet_Cute.Character_Horn_Girl
        }
    imageSize = spriteSize(tab4[1])
    tab5={}
    for z=1,#tab4 do
        a=math.random(#tab4)
        table.insert(tab5,tab4[a])
        table.remove(tab4,a)
    end
    scaler = .50
end

function draw()
    background(98)
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)
    end
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end

    
       fill(255)
    if ElapsedTime-et>1 then
        count = count - 1
        et=ElapsedTime
    end
    
   if count<=28 then
        for a,b in pairs(tab3) do
    sprite(tab5[a],b.x,b.y,imageSize*b.z)
            end
        end

     text(count,WIDTH*.10,HEIGHT*.10)
end

function touched(touch)    
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end