Skip to content
Sign upLog in
← Back to Community

How to pick a random number in C.

Profile icon
Arabica

If you want a guess a number game, (to the ten thousands). Then this post is for you! This relies on an asm() command called "rdtsc". Which is used for random numbers or time (shift eax to the left 32 times to do this). Here is the function:

unsigned short rand() { unsigned short c; // We are going to set the value of eax to c after "rdtsc" __asm__("rdtsc\n" : "=a" (c)); return c; }

That is it!

Voters
Profile icon
IvanFuentes
Profile icon
Ibraheemhunaity
Profile icon
bornengineer
Profile icon
JulianoEletroni
Profile icon
slanza
Profile icon
DUDEMASTER169
Profile icon
DanielRDorado
Profile icon
sequicode8
Profile icon
Highwayman
Profile icon
Arabica
Comments
hotnewtop
Profile icon
wynnepaton

Hi,

A really stupid question here!

Does any of the code specify the range of the random number generated?

Thanks,

Catherine

Profile icon
Highwayman

I stand corrected lol.