Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Generate non-duplicate number within a wide range
Old 02-17-2009, 05:02 AM Generate non-duplicate number within a wide range
Average Talker

Posts: 19
Trades: 0
I recent asked about the random image generator thing, and I got it to work by using a random number generator.

Here is my current code:

Code:
<?php
// here is the dir of images, images are named "filename_###-thumb.jpg"
//where ### is a sequence number of 001 - 999
$dir = "thumbs/";

// here, it counts how many files are in the dir
$total_files = count(glob($dir . "*.jpg"));

// here is the number generator in three digits
$int = str_pad(mt_rand(1, $total_files), 3, 0, STR_PAD_LEFT);

// and here is the echo of the random image where $int is the random 3 digit number
echo '<a href="/games/sf4/screenshots/char_trailer/full/char_'.$int.'.jpg"><img src="/games/sf4/screenshots/char_trailer/thumbs/char_'.$int.'-thumb.jpg"></a>';

?>
problem i am having with this is, it will generate duplicate numbers. meaning that if i want to display 6 random images in a 3x2 table, it will have a duplicate here or there. although the range of numbers is wide, but duplicates will still happen.

is there a way for me to randomize those number without duplicates? i've checked all over the web but they only supply non-duplicate number in a small range like 1 - 10.
rogerchin85 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-17-2009, 07:12 AM Re: Generate non-duplicate number within a wide range
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You will need to save each generated numbers into an array, and regenerate again and again until you have no clash.

This code is in python, but just to give you an idea:
Code:
import random

shooters={}
while len(shooters)<4:
  id=random.randint(0,9999999)
  while id in shooters.keys():
    id=random.randint(0,9999999)
  #end while
  shooters[id]=True
#end while
print shooters
I use something like this to check availability of threads in a daemon.
When the thread is active, there is an shooters[id] value. When activity stops, the threads update shooters to remove it's id.
I needed random values without clash for this to work without conflicts.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 02-17-2009 at 07:13 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Generate non-duplicate number within a wide range
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30362 seconds with 12 queries