karthikeyan_coder Posted April 22, 2007 Share Posted April 22, 2007 a program that generates a list of 10,000 numbers in random order each time it is run. Each number in the list must be unique and be between 1 and 10,000 thanks. Link to comment https://forums.phpfreaks.com/topic/48089-a-help/ Share on other sites More sharing options...
genericnumber1 Posted April 22, 2007 Share Posted April 22, 2007 I doubt someone would just write the code for you, unless they're feeling overly generous. Try to write it yourself and come back to ask us questions, if you want someone to write the script for you post in the freelance forum. Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235024 Share on other sites More sharing options...
Navarr Posted April 22, 2007 Share Posted April 22, 2007 <?php function random_tenthou_gen() { $numlist = array(); for($i = 0;$i < 10,001;$i++) { $numlist[] = rand(1,10000); } return $numlist; } ?> Should do what you want. Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235026 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 @Navarr, the for loop should be for($i = 0;$i < 10000;$i++) { But if you look at the results, uniqueness criteria is not met <?php function random_tenthou_gen() { $numlist = array(); for($i = 0;$i < 10000;$i++) { $numlist[] = rand(1,10000); } return $numlist; } $a = random_tenthou_gen(); /** * Analyse results */ $ka = array_count_values($a); $kb = array_count_values($ka); ksort($kb); $tot=0; foreach ($kb as $occ => $count) { echo "$count numbers occurred $occ times<br>"; $tot += $count*$occ; } echo "<br/>TOTAL: $tot"; ?> Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235119 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 a program that generates a list of 10,000 numbers in random order each time it is run. Each number in the list must be unique and be between 1 and 10,000 thanks. try $a = range(1,10000); shuffle($a); Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235122 Share on other sites More sharing options...
jchemie Posted April 22, 2007 Share Posted April 22, 2007 Well the prev one was a beauty. One more logic i find good is as under error_reporting(0); while(count($nos) < 10000) { $no = rand(1, 10000); if(!(in_array($no,$nos, true))) { $nos[] = $no; } } Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235297 Share on other sites More sharing options...
Glyde Posted April 22, 2007 Share Posted April 22, 2007 Here: <?php function randomArray($amount=10000) { $usedNumbers = array(); for ($i = 0; $i < $amount; $i++) { $randomNumber = rand(1, $amount); while (in_array($randomNumber, $usedNumbers)) $randomNumber = rand(1, $amount); $usedNumbers[] = $randomNumber; } } ?> It's going to be slow as hell trying to do 10,000 though. Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235301 Share on other sites More sharing options...
genericnumber1 Posted April 22, 2007 Share Posted April 22, 2007 I prefer Barand's approach, here it is in function form... <?php function randArray($from = 1, $to = 10000) { $numbers = range($from, $to); shuffle($numbers); return $numbers; } ?> Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235418 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 It's going to be slow as hell trying to do 10,000 though. Only 750 times longer than my method (15 sec vs 0.02) Link to comment https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235419 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.