Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weighted randomisation.

Status
Not open for further replies.

Jacqui1811

Programmer
Jul 8, 2002
100
SG
I am trying to do a flashcard project to help learn a langauge and I have all the words in a category in a nice array in the format :

key -> combination_id , value -> 1
key -> eng_word, value -> cat
key -> for-word, value -> neko
key -> skill_level, value -> 3

I have a lot of these in the array and want to randomly pick one from it BUT I want to be able to pick those with the lower skill level (1) more often than those with a skill level of (2) and level (3) even less.

Can anyone help ?

Thanks.
Jacqui.
 
How about a 2-dimensional array? Below isn’t super elegant, but I think it would work.


$lvl1=array
(
key -> combination_id , value -> 1
key -> eng_word, value -> cat
key -> for-word, value -> easy
);

$lvl2=array
(
key -> combination_id , value -> 1
key -> eng_word, value -> cat
key -> for-word, value -> medium
);


$lvl3=array
(
key -> combination_id , value -> 1
key -> eng_word, value -> cat
key -> for-word, value -> hard
);

$word_list=array ($lvl1,$lvl2,$lvl3);

// this assumes lvl 3 10% of the time, lvl 2 40% of the time and lvl1 50% of the time
function pickWord()
{
global $word_list

$level=rand(1,10);
switch ($level)
{
case 1:
$which=rand(0,count($word_list[2]-1);
$hardness=2;
break;
case >4:
$which=rand(0,count($word_list[0]-1);
break;
$hardness=0;

default:
$which=rand(0,count($word_list[1]-1);
$hardness=1;
break;
}
return ($word_list[$hardness][$which]
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top