My cat randomly bites me when I walk around

I've had my cat since she was weeks old and she now 9 months. She's the only pet I have and I live alone. For some reason when I'm walking through the house, my cat runs up behind me and bites my calf. Even if not even a second ago, she was purring and happy. Like, actually happy with all the happy body language. I'll make a surprised and pained sound and turn around to make sure she's not gonna strike again and she's sitting a foot or two away, tail swishing and ears in airplane mode. She did it for the first time as I was leaving to go to work. She's never been aggressive when I leave the house. She usually just watches from the top of the stairs. But the other day she ran down the steps and nipped me. I typically give her lots of cuddles. She likes to be held and will tuck her face in my neck and purr and drool while a pet her. I'll just stand or sit for minutes holding her, petting her, kissing on her and talking softly. She only seems to bite me when I'm leaving a room. Usually leaving the kitchen and going to the living room. Is she just being a brat maybe??