Ask your own question, for FREE!
Mathematics 23 Online
OpenStudy (calculusxy):

Need help with C Programming!

OpenStudy (calculusxy):

How do you make C choose a random number between a range (for example 2 to 6)?

OpenStudy (xlpoisonpotato):

dude this is mathematics not programming

OpenStudy (cazeh):

lol

OpenStudy (calculusxy):

But where do I possibly ask a programming question? The Computer Science section is not even so active.

OpenStudy (cazeh):

Then, English

OpenStudy (calculusxy):

That doesn't make any sense.

OpenStudy (cazeh):

sry

OpenStudy (xlpoisonpotato):

idk man

OpenStudy (xlpoisonpotato):

google?

OpenStudy (tkhunny):

There's enough mathematics in this question, I think. Are we rolling dice? Use the rand() function. This should produce [0,1] It uses a fixed seed, so it you want it to be more random, you should use srand(unisgned int seed) . Default seed is 1. Careful how you manipulate this. If you multiply by 6 and round, you may not get what you expect.

OpenStudy (calculusxy):

@tkhunny Here's a program that I wrote to generate a random number between 2 and 6. #include <stdio.h> { int random_number; srand(time(NULL)); random_number = rand() % 7 + 2; printf("%d\n", random_number); return 0; } But I get an error when I compile it. Can you please check it?

OpenStudy (cazeh):

Click the key and r It will open Command run, then go to roaming then you can use it.

OpenStudy (tkhunny):

Two things: 1) Is TIME() integer? 2) Why would % 7 + 2 be [2-6]? I don't know much about it. C isn't really where I live.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!