one rand function instead of 9 seperate ones to generate 9 seperate results

Discussion in 'PHP' started by Herpsy, Feb 1, 2012.

  1. #1
    this forum is being strange and i cannot figure out how to add code blocks. Ill go somewhere else. thanks tho
     
    Solved! View solution.
    Last edited: Feb 1, 2012
    Herpsy, Feb 1, 2012 IP
  2. aidanriley

    aidanriley Banned

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    51
    #2
    Hey, don't leave us yet. I think they allow you to post code blocks are reaching 15 posts or something like that. Spam issue.
     
    aidanriley, Feb 1, 2012 IP
  3. Herpsy

    Herpsy Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #3
    thanks for the respectful response.

    what i want really is a loop. im guessing a foreach, that creates a new numbered var and appends a random number at the end of that var from 1 to 9 but it would only be one var together. i think i need an array, but the numbers have to be repeatable too. not just used once. the output needs to be

    $var1$randomnumber
    $var2$randomnumber
    $var3$randomnumber
    $var4$randomnumber
    $var5$randomnumber
    $var6$randomnumber
    $var7$randomnumber
    $var8$randomnumber
    $var9$randomnumber

    so the first part of the var needs to be an ordered number from 1 to 9, just like you see tgere. but the second part of the var needs to be a random number from 1 to 9 with the rand function. the numbers need to be repeatable so like the number 8 could show up twice, etc. im trying to randomly display .jpg images from a directory in a 3 x 3 grid. Hopefully this explains it

    edit: what i was doing was using the rand function right above the img src each time. but for what i want that wont work. the vars need to be all created before the img src is used.
     
    Herpsy, Feb 1, 2012 IP
  4. #4
    How about an array? :)

    
    <?php
    
    
    	// How many numbers?
    	$limit = 9;
    	$minNumber = 0;
    	$maxNumber = 100;
    	
    	
    	$numbers = array ();
    	
    	// Now, save a random number in each space
    	for ( $x = 1; $x <= $limit; $x ++ )
    		$numbers[$x] = rand ( $minNumber, $maxNumber );
    	
    	// Print them
    	print_r ( $numbers );
    	
    ?>
    
    Code (markup):
     
    maureeeteeeee, Feb 1, 2012 IP
  5. Herpsy

    Herpsy Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #5
    holy crap christ replying on this forum is insanly annoying sometimes. Your sessions are too short, the preview buttons explode anything related to code even without the code blocks. Damn. But still, it has awesome support. Anyway, would need to adjust the code a bit abovewith $minNumber being 1 and $maxNumber being 9but i am assuming the end result would be 9 created vars$numbers1$numbers2$numbers3$numbers4$numbers5$numbers6$numbers7$numbers8$numbers9with each assigned a random number between 1 and 9 I might need to change the value of $x to 0 instead of 1 but other then that I think this is what I need. Unless I am wrongI cannot check as I am at work. Attempt 5 to reply nowedit: your preview button is broken as there is no php code in this reply.
     
    Herpsy, Feb 1, 2012 IP
  6. Herpsy

    Herpsy Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #6
    wow that was not supposed to happen. Sorry for the formatting of the above post. Ill deal with it after work. thanks for the support
     
    Herpsy, Feb 1, 2012 IP
  7. maureeeteeeee

    maureeeteeeee Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    41
    #7
    Yeah, what you're trying to do

    $var1
    $var2

    is obnoxious and not good code
    Using an array improves your performance and speed
     
    maureeeteeeee, Feb 1, 2012 IP
  8. Herpsy

    Herpsy Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #8
    I need it that way tho, im assuming the array is the correct way to do what I need. I am fine with what I call "explodie code" I just build for fun and am self taught from about 7 years or so. As long as my projects look good, I can care less how its created. I get better over time and look back at my rook code and laugh. I was fine with manually creating the vars and using the rand function 9 times but the issue I ran into was that I needed all 9 random numbers already generated to determine if the photos were going to come from directory A, or Directory B. if i was just using one directory my explodie code I would have been happy with. I do appriciate all your help and will test your code in about 2 hours. Ill even pm you my project just for fun. Thanks
     
    Herpsy, Feb 1, 2012 IP