It is narrated from Sayyiduna Hudhayfah bin Asid (may Allah be pleased with him) that he said: I heard the Messenger of Allah (peace and blessings be upon him) say: "When the drop of semen settles in the womb and forty nights pass over it, the angel comes to it and says: 'O my Lord, is it male or female?' So the Lord, the Mighty and Majestic, dictates, and the angel writes it down. Then he says: 'What is its provision? And what is its lifespan?' The Lord of the worlds dictates, and the angel writes it down. Then he says: 'Is it wretched or blessed?' The Lord, the Mighty and Majestic, dictates, and the angel writes it down; then he rolls up the scroll."
Hadith Referenceالمعجم الأوسط / باب الألف / 1535
Hadith Takhrij«أخرجه مسلم فى «صحيحه» برقم: 2644، 2645، وابن حبان فى «صحيحه» برقم: 6177، والبيهقي فى «السنن الكبرى» برقم: 15521، وأحمد فى «مسنده» برقم: 16392، والحميدي فى «مسنده» برقم: 848، والطحاوي فى «شرح مشكل الآثار» برقم: 2663، 2664، والطبراني فى «الكبير» برقم: 3038، 3036، 3039، 3040، 3041، 3042، 3045، 3043، 3044»