Bucky, thanks for the tutorials. They are very helpful. I've got a question. In this tut you showed us how to use srand. How do you limit the generated random numbers to a given interval (say, a number between 1 and 100)? In the previous tut, you taught us that the formula 1+rand()%100 would do the trick. But I tried to modify this tut to make the program generate a seed random number between 1 and 100, and I got the same number 15 times.
Your C Programming Tutorial 13 about Seeding Random Numbers is really awesome.
After watching your video i tried & made my own random number program using your same code.
The Problem is i wanted the program to loop, so i did a while loop, by doing this my program generates random numbers only the first time when its executed & not during my while loop.
The program outputs the printf statement but without random number just goes to the while loop statement again.
Your C Programming Tutorial 13 about Seeding Random Numbers is really awesome.
After watching your video i tried & made my own random number program using your same code.
The Problem is i wanted the program to loop, so i did a while loop, by doing this my program generates random numbers only the first time when its executed & not during my while loop.
The program outputs the printf statement but without random number just goes to the while loop statement again.
@ttognala no. the point is that an algorithm cannot produce truly random numbers. therefore inputting a number that is returned from an algorithm is going to give the same output.
Of course, you could argue that there's no such thing as a random number full stop, as when a human picks one there are always factors influencing that decision. What does it expect me to choose? What haven't i chosen in a while?
@zaidento couldn't you just use the tip he used in the previous video? you use the % sign to calculate the remainder with 10 different values, and then add 20?
@bondservant4Him :) Thanks for the concern :) I already fixed it. I copied the exact code up there. But I forgot to remove the scanf function from my previous project (Im using the same sheet from my previous).
I've tried both " srand(time(NULL)); " and " srand(time(0)); " (obviously without the " " ) -both seem to generate 0's and no actual numbers - I'm using xcode 3.2.6 - and I'm working in a standard main.c file... does anyone know why this would be generating a bunch of 0's? (fyi - I have it looped like 30 times, and it's all 0's fml)
@TrancendedTora NULL is not defined in the C standard library. Use 0 (zero) instead, or after your includes add the line #define NULL 0 and it will work. Some C implementations will define it, but not all, so that may be why it works for the author and not for you.
Good trick! That was pretty random, but what happened to the first number? I would like to know. I suppose getting a totally random number is impossible after all. But good try! Thanks for the explanation and keep the good work!
@rapidopato The first numbers were different; on the first run it was 9930 and on the second run it was 9972. They may not be far off from one another, but still different, and moreover if you were to use the modulo operator to get a random number between 0 and 9, [rand() % 10] you would get 0 back in the first run and 2 back in the second run.
visit technoexam website for more details on c programming !
large program library, source codes, examples with output, header files, file handling, lecture notes, study notes, question papers, answers, quiz, objectives quesionts and many more !!!!!!!!!!!!!!
Thank You. Thank You so much for uploading this. I can now finish my HW and have a lot of fun with it. You are a genius. I've already subscribed to learn some more from you. ^_^
If u have problem because compiler says " `time' undeclared (first use this function) " well u have to add this # include <time.h> and if rand and srand doesnt work you have to add # include <stdlib.h> nice work Bucky keep it on!
this is a good explanation. I was reading about srand and didn't quite understand how the number was selection was called by the input of time. Visually helped me out here. Don't quite understand what the NULL purpose is. I have seen this done srand (time(0)) what is the difference?
however when i click the roll button, the same letters keep coming up, and they dont change, i need it to keep on randomly displaying different letters.
@Digadogup if you do srand(rand()) it will give you the same numbers every time you run the program. the time() function gives you unix time which is different every second (for example unix time right now is 1267392718 in Toronto, Ontario)
@icekat83 return 0 is used to tell the main function that your not returning anything to it (because he put main's return parameters as 'int') just put void main to avoid using return 0
In lecture one day, prof was talking about using random numbers that seed from the electricity gauged in a circuit to the computer. IDK, how that could be implemented, but that seems pretty damn random if you could get it to work!
great video, if you need any help getting your video/channel exposed i use a site called mytubeviews . com It has helped 5 of my videos get ranked on the first pages.
Check out my giveaway. Giving away iPod touch and iPhone prizes worth $1770. Sign up now. You have nothing to lose. Check out my channel now for the giveaway!!!
If you can tell by my Username, I'm more of a Java freak. But, if I'm correct, Java derived from C, so I should know the basics already. Except, I noticed you, could also do something like this.
can we make it a random case whileloop?
hardstylegronne 17 hours ago
i love ur jumber numberator :)
hgfjyrdetryu 1 month ago in playlist C Programming Tutorials 5
@ttognala U can always try it, for me it doesn't work
mjelma31 1 month ago
Make more C tutorials please Bucky!
JDOG13596 1 month ago
time identifier not found keeps coming up in my program, whats the story with that?
SuperMulder10 1 month ago
Bucky, thanks for the tutorials. They are very helpful. I've got a question. In this tut you showed us how to use srand. How do you limit the generated random numbers to a given interval (say, a number between 1 and 100)? In the previous tut, you taught us that the formula 1+rand()%100 would do the trick. But I tried to modify this tut to make the program generate a seed random number between 1 and 100, and I got the same number 15 times.
SkinnyJeff 1 month ago in playlist C Programming Tutorials - The New Boston
This has been flagged as spam show
Your C Programming Tutorial 13 about Seeding Random Numbers is really awesome.
After watching your video i tried & made my own random number program using your same code.
The Problem is i wanted the program to loop, so i did a while loop, by doing this my program generates random numbers only the first time when its executed & not during my while loop.
The program outputs the printf statement but without random number just goes to the while loop statement again.
UmairRazak7 1 month ago
This has been flagged as spam show
Your C Programming Tutorial 13 about Seeding Random Numbers is really awesome.
After watching your video i tried & made my own random number program using your same code.
The Problem is i wanted the program to loop, so i did a while loop, by doing this my program generates random numbers only the first time when its executed & not during my while loop.
The program outputs the printf statement but without random number just goes to the while loop statement again.
UmairRazak7 1 month ago
Jumber generator :D love your jokes and tutorials
Redukas123 1 month ago
bagatelox 1 month ago in playlist C Programming Tutorials
@ttognala no. the point is that an algorithm cannot produce truly random numbers. therefore inputting a number that is returned from an algorithm is going to give the same output.
jaywbe 2 months ago
For those who use Linux (GCC) use stdlib.h and time.h instead conio.h. Anyway, thank you Bucky ;)
19n05k83 2 months ago
but in linux there will be no conio.h
saa442 2 months ago
Awesome... srand along with time!
angadiabhi057 4 months ago
hallu guys ahm.. how am i going to arrange the random numbers from least to greatest?? would you help me/?
netskerful 4 months ago
u r awesome
sanausmani08 4 months ago
Implicit declaration of function time
LosEagle 5 months ago
WTF!?
it was much easier and different when using BORLAND C
4471548nwn 5 months ago
"NICE!"
xsxgotenxsx 5 months ago 2
is there any way to assign the random value to a variable? like if i wanted to get a random number, but then use the same random number later on.
Xenocide007 5 months ago
Comment removed
XcXtrippyXcX 5 months ago
@XcXtrippyXcX thanks bro, dunno why your comment got removed though :P
Xenocide007 5 months ago
Hi there Bucky, and the rest of you!
You have really good videos, and everything has gone good until now for me. "srand" works fine, but when I add the time parameter, it wont work!
I included stdlib.h
This is my output: srand(time(NULL));
........^
%CC-I-IMPLICITFUNC, In this statement, the identifier "time" is implicitly decla
red as a function.
at line number 6 in file SYS$SYSDEVICE:[EGHURTIG.CC.CPT1]PROGRAM.C;4
I guess I should include some additional header, but I don't know which! :)
ehurtig006 6 months ago
@ehurtig006 try including time.h
bondservant4Him 6 months ago
@bondservant4Him
Yeah, thank you! Actually I already tried that and it worked great!
I meant to reply to myself with this info, but I couldn't post anything more at the time.
Nevertheless, thank you very much! :)
ehurtig006 5 months ago
Of course, you could argue that there's no such thing as a random number full stop, as when a human picks one there are always factors influencing that decision. What does it expect me to choose? What haven't i chosen in a while?
SepradistPhantom 6 months ago
hi how to generate random numbers with a range? hm lets say i wan the random numbers to be within a range of 20 - 30? thx. (:
zaidento 6 months ago
@zaidento Watch the tutorial just before this one, and the answer to your question is "20+rand()%11" rather than "rand()"
TheHoho001 6 months ago
@zaidento couldn't you just use the tip he used in the previous video? you use the % sign to calculate the remainder with 10 different values, and then add 20?
ehurtig006 6 months ago
Thanks again!
WinXPproff 6 months ago
I have a question: Why my program doesn't stop generating numbers. I actually used the same code up there. What is the solution>?
jodzjcm 6 months ago
@jodzjcm pm me ur code ill look at it
bondservant4Him 6 months ago
@bondservant4Him :) Thanks for the concern :) I already fixed it. I copied the exact code up there. But I forgot to remove the scanf function from my previous project (Im using the same sheet from my previous).
jodzjcm 6 months ago
@jodzjcm sounds like i doesn't reach the limit, is it definately incrementing on each loop?
Gazareth2000 6 months ago
have some fun whit the tutorial...:D
dautmolahasanov 6 months ago
My "random" numbers are exactly the same...they are really random...:D
dautmolahasanov 6 months ago
want a really random number generator try this for the srand function. srand(rand() * srand());
and just use any operator you want.
ssjmysticgohan 7 months ago
@ssjmysticgohan I tried that and put time(NULL) inside both - got the same numbers everytime, any ideas why?
DixonNxn 7 months ago
@DixonNxn not sure let me test that out again. i'm doing networking now so i lost some of what i was learning about C
ssjmysticgohan 5 months ago
i got 41 in the first number :O
robertosygal 8 months ago
I've tried both " srand(time(NULL)); " and " srand(time(0)); " (obviously without the " " ) -both seem to generate 0's and no actual numbers - I'm using xcode 3.2.6 - and I'm working in a standard main.c file... does anyone know why this would be generating a bunch of 0's? (fyi - I have it looped like 30 times, and it's all 0's fml)
LukiPooki 9 months ago
@LukiPooki send me your code ill take a look at it
bondservant4Him 9 months ago
rofl, your brain did use some random pattern to generate words "jumber numberater"
1Alino 9 months ago
Thanks! I'm subscrived. You convinced me! Great tutorials!
theral056 9 months ago
help pls, `time' undeclared (first use this function)
tyilmazdogan 10 months ago
Sorry, my iPod seems to replace greater than and less than signs with hyphens
ian9730 10 months ago
#include -time.h-
(You forgot the last greater than sign)
ian9730 10 months ago
Hi, nice video, for those who facing time() error, add #include <time.h
promosys 10 months ago
why do you keep using the while loop where the for loop shines?
TheGamingDude23 11 months ago
This has been flagged as spam show
srand(time(NULL)); doesn't work for me
TrancendedTora 11 months ago
srand(time(NULL)); doesn't work foe me
TrancendedTora 11 months ago
@TrancendedTora NULL is not defined in the C standard library. Use 0 (zero) instead, or after your includes add the line #define NULL 0 and it will work. Some C implementations will define it, but not all, so that may be why it works for the author and not for you.
pearcewf 11 months ago
the time(NULL) function didn't work so well with me ...why ??
alaahima 1 year ago
Good trick! That was pretty random, but what happened to the first number? I would like to know. I suppose getting a totally random number is impossible after all. But good try! Thanks for the explanation and keep the good work!
rapidopato 1 year ago
@rapidopato The first numbers were different; on the first run it was 9930 and on the second run it was 9972. They may not be far off from one another, but still different, and moreover if you were to use the modulo operator to get a random number between 0 and 9, [rand() % 10] you would get 0 back in the first run and 2 back in the second run.
pearcewf 11 months ago
I researched wooping cough, and it is pretty bad... Best get that checked out.
Mexicouger 1 year ago
visit technoexam website for more details on c programming !
large program library, source codes, examples with output, header files, file handling, lecture notes, study notes, question papers, answers, quiz, objectives quesionts and many more !!!!!!!!!!!!!!
---- technoexam - online website
dineshkudache 1 year ago
thanks!
Digivice 1 year ago
Jummer generator? Jumper generator? Where are we jumping to Buck. LOL
Shorty20122012 1 year ago
Thank You. Thank You so much for uploading this. I can now finish my HW and have a lot of fun with it. You are a genius. I've already subscribed to learn some more from you. ^_^
Jadandlud 1 year ago
@IsTI37Cluj
nice game!
aradScream 1 year ago
At 2:32 he's about to cough!
GandhiaLasVegas 1 year ago
cant you do srand(A)
A == 0
and then a++
god4506 1 year ago
@god4506
no, it just run one time at all , so the result are always same as the frist time
roymandwoo 1 year ago
Haha no more wooping cough :P
Bansihm3nt 1 year ago
I see....
lazarus554 1 year ago
That's not truly random number, it still follow pattern, you need to add more randomness to srand in order to get more truly random number
ignas2526 1 year ago
If u have problem because compiler says " `time' undeclared (first use this function) " well u have to add this # include <time.h> and if rand and srand doesnt work you have to add # include <stdlib.h> nice work Bucky keep it on!
Thorgils1 1 year ago
@Thorgils1 why does he uses conio.h, is it necessary?
manutube8080 1 year ago
@manutube8080 hey conio.h is used to allow getch();
Thorgils1 1 year ago
@Thorgils1 no its not. i erased my conio.h and worked fine.
erikizitoxxx 1 year ago
this is a good explanation. I was reading about srand and didn't quite understand how the number was selection was called by the input of time. Visually helped me out here. Don't quite understand what the NULL purpose is. I have seen this done srand (time(0)) what is the difference?
paulliptrott 1 year ago
i miss the line; "Sorry its my whopping cough".
savezera 1 year ago 44
@savezera rofl, although he says "whooping cough" which it definitely isn't as that's a pretty serious disease :-)
HootsMcGruff 1 year ago
the problam that the random number just going up and not really random :(
tigerLRg245 3 months ago
This has been flagged as spam show
jumber numberater haha :)
cSin6 1 year ago
Coment out the 'seed' can sometimes be helpful during testing so that fixed Random Numbers are produced for testing against.
Great and Helpful Video's
Films4You 1 year ago
That's very good tutorial!!
Rikimaru168 1 year ago
i am doing a dice game, i have created 5 panels, and i want to randomly generate a letter in each of those panels,
static char *Symbols[]={"A","K","Q","J","P","S"};
either 1 of those letters.
however when i click the roll button, the same letters keep coming up, and they dont change, i need it to keep on randomly displaying different letters.
reece02009463 1 year ago
Random things:
FAN SPEED RPM
CPU TEMP
ipromisenot 1 year ago
its really helpful thanks :)
hourfung 1 year ago
10q so much for z explanatuion B.
asorit 1 year ago
lol so after 24 hours you'l get the same al last time
sweet vid im noob so im going to surgest u do srand(rand())
Digadogup 2 years ago
@Digadogup if you do srand(rand()) it will give you the same numbers every time you run the program. the time() function gives you unix time which is different every second (for example unix time right now is 1267392718 in Toronto, Ontario)
dNullMe 1 year ago
it doesnt compile
darylrasco 2 years ago
this does not work, it keeps returning the same value all the time because you did it wrong. here is an easier way:
int random(int max)
{ int seed = timeGetTime(); return (seed % max);
}
usage:
int y = random(4); //generates number from 0-4
HordeAreMyFatCousins 2 years ago
`srand' undeclared (first use this function)
`time` undeclared (first use this function)
7OBBExXxINTEE 2 years ago
`srand' undeclared (first use this function) `time` undeclared (first use this function)
I gues these errors when I compile and run, any help? DevC++ and VC++ both give the same error.
BufferOverflowAttack 2 years ago
Ok I really need to look at the title of the next tutorial before making comments!
Piece of trivia. I tried srand(srand()) which seemed to work quite well. LOL.
BTW Why has 'return 0;' suddenly appeared? What's it for?
icekat83 2 years ago 2
@icekat83 return 0 is used to tell the main function that your not returning anything to it (because he put main's return parameters as 'int') just put void main to avoid using return 0
chadchad125 1 year ago
Also, very useful videos (y)
jindak126 2 years ago
Haha I love you're 'Wooping cough' and Jumber Numberator! :)
jindak126 2 years ago
It keeps saying 8 `time' undeclared (first use this function)
juha686 2 years ago
Quanantum mechanics
n7275 2 years ago
jumber numberater
0871356005 2 years ago 63
your a cool kid
hrhodes3 2 years ago
I can understand you better when you say "As you can see" about the jumber numerator...
newmanto03 2 years ago
lol
persianqu33n 2 years ago
In lecture one day, prof was talking about using random numbers that seed from the electricity gauged in a circuit to the computer. IDK, how that could be implemented, but that seems pretty damn random if you could get it to work!
MurderedAnimalFood 2 years ago
This has been flagged as spam show
great video, if you need any help getting your video/channel exposed i use a site called mytubeviews . com It has helped 5 of my videos get ranked on the first pages.
Sweet! Sweet! Sweet!
Deontefh 2 years ago
Thanks a lot for this tutorial! :)
Shortmandesigner 2 years ago
awesomee:)
rubioelpayazo 2 years ago
This has been flagged as spam show
Check out my giveaway. Giving away iPod touch and iPhone prizes worth $1770. Sign up now. You have nothing to lose. Check out my channel now for the giveaway!!!
iTonemod 2 years ago
what recording software are you using??
VampieGames 2 years ago
camtasia
thenewboston 2 years ago
@VampieGames dev ++
monguiller 9 months ago
Sweeeet! Thanks Bucky!
ddoodm 2 years ago
Also, you could just make an applet, and handle a max number, then make a few classes to handle randomized paths.
TonyTheJavaGuru 2 years ago
Ehh, I'm not really a C fan.
If you can tell by my Username, I'm more of a Java freak. But, if I'm correct, Java derived from C, so I should know the basics already. Except, I noticed you, could also do something like this.
int rand()
{ random_seed = random_seed * 1103515245 +12345; return (unsigned int)(random_seed / 65536) % 32768;
}
TonyTheJavaGuru 2 years ago
erm.. java is like c with oop, all in a sandbox.
MurderedAnimalFood 2 years ago
ohhh i gettt it
:)
Metalheadplus1 2 years ago