Need help with C Programming!
How do you make C choose a random number between a range (for example 2 to 6)?
dude this is mathematics not programming
lol
But where do I possibly ask a programming question? The Computer Science section is not even so active.
Then, English
That doesn't make any sense.
sry
idk man
google?
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.
@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?
Click the key and r It will open Command run, then go to roaming then you can use it.
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.
Join our real-time social learning platform and learn together with your friends!