Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1084 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Choose random

Thread title: Choose random
Closed Thread    
    Thread tools Search this thread Display Modes  
05-25-2006, 08:59 PM
#1
.Nick is offline .Nick
.Nick's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,159
iTrader: 3 / 100%
 

.Nick is on a distinguished road

  Old  Choose random

Say I have a variable $cnt with the number of cells in a table. The content of each cell is determined by $array[$i]. With PHP, how would I go about picking 3 random cells and and replacing their content with something else?

05-25-2006, 09:25 PM
#2
Jonny is offline Jonny
Status: Member
Join date: Feb 2005
Location: UK
Expertise:
Software:
 
Posts: 335
iTrader: 0 / 0%
 

Jonny is on a distinguished road

  Old

PHP Code:
$arraycount count($array);

for(
$i 0$i 3$i++) // note the 3 here
{
     
$random rand(0$arraycount);
     
$array[$random] = "New Content :O!";

Probably can be done lots better though. (by smarter people) It also wouldn't stop the content from being changed twice. :P

05-25-2006, 09:31 PM
#3
.Nick is offline .Nick
.Nick's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,159
iTrader: 3 / 100%
 

.Nick is on a distinguished road

  Old

How do you keep rand from choosing the same number twice?

05-25-2006, 09:36 PM
#4
Jonny is offline Jonny
Status: Member
Join date: Feb 2005
Location: UK
Expertise:
Software:
 
Posts: 335
iTrader: 0 / 0%
 

Jonny is on a distinguished road

  Old

You would have to store the chosen random numbers somewhere and then check the new random numbers against them. (or sth)

I can only think of a really hacky way of doing it.

05-25-2006, 09:49 PM
#5
.Nick is offline .Nick
.Nick's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,159
iTrader: 3 / 100%
 

.Nick is on a distinguished road

  Old

PHP Code:

$cnt 
count($games);

while(
$l <= 3// note the 3 here
{
    
$random rand(0$cnt);
    if (
$games[$random] != $adcode) {
        
$games[$random] = $adcode;
        
$l++;
    }

Why is that an infinite loop?

05-25-2006, 10:06 PM
#6
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

parhaps all the elements in games are equal to adcode or there is only one element in games and it is equal

05-26-2006, 01:20 AM
#7
.Nick is offline .Nick
.Nick's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,159
iTrader: 3 / 100%
 

.Nick is on a distinguished road

  Old

Neither of those are the case

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed