You cast the card before shuffling your library (if it was searched). In some very rare cases, you may need to take actions involving your library while casting the card. For example, if it has an additional cost and you activate Deranged Assistant's ability ("{T}, Put the top card of your library into your graveyard: Add {C}.") to pay for it, you must maintain the order of your library while you search it.