The rand() function shall always return a number between 0 and RAND_MAX on the system. To find this "RAND_MAX" number, you simply do:
printf("My rand() max is %d\n", RAND_MAX);
And it should print it out. For instance, mine is 2,147,483,647, which coincidentally is the largest possible integer too. For Windows, however, it is only 32767, which is quite a shame.
@PH5221 stdlib supports different functions for example malloc-dynamic memory allocation, if you are just starting out in C stdlib is not really needed stdio will do fine
the reason why you have the same random number result is that the location of your data type is the same.. try using "FLOAT", if result will remains the same. try restarting your computer for its regenerate random location on your system.
the reason why it does randomize number from 0-32,000+ because the values inside the data type "INT" is equal to 0-32,727 i guess. the maximum number doesn't depends on your system but it depends on the data type your are using. ^^ correct me if I'm wrong
I don't get how this is a random function. You get 41, I get 41, re run it 20 more times I get 41. I continue with this tutorial and do the while loop, I get the exact same numbers as you no matter how many times I re-compile.
It gives me the EXACT same numbers every time! I have it set to give me three random numbers (1 - 10) and it gives me 2, 8, then 5 in that order! Please help me out!
It gives me the EXACT same numbers every time! I have it set to give me three random numbers (1 - 10) and it gives me 2, 8, then 5 in that order! Please help me out!
can you please explain this "%10" thing again? i dont really understand this...it devides the random number but why it always is the number that "smooth" then? like when i pick 5/10 or so it wouldnt work or what?
In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the following comments are made: "If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); and never by anything resembling j=1+(rand() % 10); (which uses lower-order bits)."
I got the same numbers as you got, so if many computer get the same random numbers I think we can crack the code for the slot machines at the casino :)
why is it always 41 is the first random number you get here..?? thats why when divided by 10., u got the remainder 1., and when by 6 and added 1 u got 6..??
a random number generated from a seed to create a number depending on what the seed is.
If you had a seen based on RTC(real time clock) and wanted random numbers between 0 and ten everytime u press the space bar, if u pressed it 1 time per second it would have 0,1,2,3,4,5,6,7,8,9,10,0,1,2,3,4,5,6,7,89,10 ect.
in this case, the seed is based on the code or number of lines (i think)
for those who are having a problem with an undeclared rand function just add #include <stdlib.h> so that the rand function can be recognized. thanks bucky ive learned this from you on lesson 1 :)
hey neuboSton hao can i make a program vith a neat interface inStead of the cmd ? can u plz make tut or can anyone tell me plz i tink i need viSual baSic?? or Something? i need a vid LOL
I can't see any differences between my code and yours, and I'm using Dev-C++ but when I go to compile and run this code it gives me the message, " 'rand' undeclared " and so it doesn't work.
What .whatever did you save it as? My text editing program, textwrangler, uses that to decide what to run it as, because it handles pythons and phps and everything, so I have to know the .ending......
@khjuiproductions thnx bro! dude did you know all the list of #include so i can just put it all every i make a script .. if you know all .. give it to me .. thanks you ^^
These are not proper random numbers. I get the exact same numbers as you and the exact same numbers each time it's run. Proper random numbers should be different each time.
the random function isn't exactly "random". The function is exactly what it says, a "function". Which means that its going to go on a specific path of operations. Just something to keep in mind.
PS when you do this code he has up here, your going to get the EXACT SAME numbers as him :D
Thats an incorrect description of what the function does. It doesn't provide a random number, but a pseudo-random number. Remember that functions are just code, that perform instructions. Nothing random about that.. just a complex algorithm.
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!!!
weird :/ it generated same numbers as your computer LOL :D
ongunsavas 2 days ago
ok to all this who the numbers are always the same do this
enter to your code
<include<time.h#
(and after the int (in this video its after int i = 1
just do :
;((srand(time(NULL
happy to help :)
RonSerruya1 1 week ago
This has been flagged as spam show
@RonSerruya1 thanks :)
Arko9858 5 days ago
Comment removed
Archaicste 5 days ago
Comment removed
RonSerruya1 1 week ago
Comment removed
RonSerruya1 1 week ago
Everyone is getting 41? I got 41 too...
omg123596 2 weeks ago in playlist C Programming Tutorials
This has been flagged as spam show
@omg123596
ok to all this who the numbers are always the same do this
enter to your code
<include<time.h#
(and after the int (in this video its after int i = 1
just do :
;((srand(time(NULL
happy to help :)
RonSerruya1 1 week ago
variable % 10; variable modulus 10. NOT DIVIDE.
you need to seed the random number generator or you will get repeat results.
linuxifed 1 month ago
The rand() function shall always return a number between 0 and RAND_MAX on the system. To find this "RAND_MAX" number, you simply do:
printf("My rand() max is %d\n", RAND_MAX);
And it should print it out. For instance, mine is 2,147,483,647, which coincidentally is the largest possible integer too. For Windows, however, it is only 32767, which is quite a shame.
frxstrem 1 month ago in playlist C Programming Tutorials
I'm running the c file through the terminal in Fedora16, but it prints out the same numbers everytime I execute it...
mjelma31 1 month ago
@mjelma31 OK you explain it in the next video.Sorry I overreacted :P THNX
mjelma31 1 month ago
ITS NOT RANDOM! :( help
sheshuke 1 month ago
@sheshuke The rand function is not "actually" random.
TromboneUSAlabama 1 month ago
It got errors saying"`rand' undeclared (first use this function) "
sheshuke 1 month ago
@sheshuke help!
sheshuke 1 month ago
@sheshuke same here
AJJSDN 1 month ago
@sheshuke hey I found the solution, include the header stdlib.h in your program.
AJJSDN 1 month ago
I got 2 billion XD
SoundslikeDisturbed 1 month ago
excellent tutorial ty for the hard work.
beehivepizza 2 months ago
i dont why but i get an error at rand function in c,im using devc++ bloodshed
(rand undeclared,first use of this function)
demyrik 2 months ago
@demyrik pound include time.h try that
bondservant4Him 2 months ago
Comment removed
redcodeful 3 months ago
for those of you who havn't figured it out yet he tells you how to get a truely random number in the next tutorial c:
Xenocide007 3 months ago
thank you ^_^ this helped me a lot
demon7s 3 months ago
It's not random... mine is same as his... weird
LadiesmanGW 3 months ago in playlist C Programming Tutorials
wtf why are my numbers same as buckys ?? wtf nott rand shit ?
mastermax7777 3 months ago
THUMBS UP IF YOU ALWAYS GOT THE SAME NUMBERS AS BUCKY!
Paraclete333 4 months ago 5
not so random, my numbers were exactly the same as buckys :P
TheJ0Ost 4 months ago
Is there any differene between stdlib.h and stdio.h? Should I include both in my code?
PH5221 4 months ago
@PH5221 stdlib supports different functions for example malloc-dynamic memory allocation, if you are just starting out in C stdlib is not really needed stdio will do fine
bondservant4Him 4 months ago
ERM the random number is no so random, i got the exact same result as bucky did xD did anyone else get that? :D
firefart3r 4 months ago
@firefart3r I got 41 as well >.< i think something went wrong xDDD
TheMedic1231 4 months ago 8
@TheMedic1231 hehee xD he explains in next vid ^_^
firefart3r 4 months ago
@TheMedic1231 actually that is not a random functiona at all. cos i got exactly the same 20 numbers in the same order. heheh
freetalk08 3 months ago
@freetalk08 same. but its close enough, if you tweaked it enopugh, it could be random...
TheMedic1231 3 months ago
This has been flagged as spam show
@TheMedic1231
ok to all this who the numbers are always the same do this
enter to your code
<include<time.h#
(and after the int (in this video its after int i = 1
just do :
;((srand(time(NULL
happy to help :)
RonSerruya1 1 week ago
the reason why the program gives you 0 result is that you are using the math function module "%", the mathematical symbol for division is "/"
-.^^
Jigz345 5 months ago
the reason why you have the same random number result is that the location of your data type is the same.. try using "FLOAT", if result will remains the same. try restarting your computer for its regenerate random location on your system.
-opinion from a 1st year student ^^
Jigz345 5 months ago in playlist TheNewBoston - C Programming
the reason why it does randomize number from 0-32,000+ because the values inside the data type "INT" is equal to 0-32,727 i guess. the maximum number doesn't depends on your system but it depends on the data type your are using. ^^ correct me if I'm wrong
Jigz345 5 months ago in playlist TheNewBoston - C Programming
yeah i agree... me too .. its not random i think... i also get same exact number..
TheNewbie27 5 months ago
Not very random, I get the exact same numbers as you...
goudewup 5 months ago 2
wooooooow i get massive numbers, ubuntu is awesome ;D
EpilepticElbow 5 months ago
fail, random number are all the same for me :c
cookiesRgood26 6 months ago 2
@cookiesRgood26 I mean it generates the same sequence of numbers every time, not that all the numbers are exactly the same.
cookiesRgood26 6 months ago
@cookiesRgood26 same for me, i 'am getting the same numbers as you are. lol
xsxgotenxsx 5 months ago
Never mind I just moved onto the next tutorial and you explain why at the beginning of that one.
Joxieify 6 months ago
I don't get how this is a random function. You get 41, I get 41, re run it 20 more times I get 41. I continue with this tutorial and do the while loop, I get the exact same numbers as you no matter how many times I re-compile.
Joxieify 6 months ago
Comment removed
Joxieify 6 months ago
I have problem with this tutorial. After typing the codes in the tutorial, my program didn't compile. I gives error messages.
sydneyozful 6 months ago
@sydneyozful
did u find iut how. if so let me know
faizaladams1 2 weeks ago
Comment removed
NozFwbit 6 months ago
Comment removed
rx3gundam 6 months ago
retardet!!!!
Use C#
misterniceking2 7 months ago
weird how random isn't really random.
djfoo000 8 months ago
@djfoo000 you have to youse srand function ... they will not be same :)
LosEagle 7 months ago
41 for me too.Not very random after all...
utuber8781 8 months ago
@utuber8781 most programming languages use seeds for random numbers for consistency of random functions
dassouki 6 months ago
I got exactly the same 15 numbers also in that specific order. Kinda scary :p
kv310 8 months ago
I get 41 every time....
guyzyl1 10 months ago 2
@guyzyl1 make sure your seeding it right
bondservant4Him 9 months ago
why return0 ; dont work?
kildas 10 months ago
OMG MY RANDOM NUMBER WAS 41 ALSO!!!!!! AAAAAAAAAAA!!! :)
brucinski94 10 months ago 2
#include
60058366 11 months ago
This not not completely random yet... you'll get the same numbers that he does.
If you want it to be truly random so u get different numbers every time, you gotta use srand() or the time() function.
IEOA 11 months ago
Just found out that you have to include stdlib.h to use the ran() function.
EVOX16 11 months ago
@EVOX16
it depends on the compiler you have. I dont need the stdlib.h to use rand() function.
PivotBeguiner 11 months ago
@PivotBeguiner
Ah, I was using XCode. Makes sense.
EVOX16 11 months ago
I sign up in youtube just to say thank you to you.. ^_^
"thank YOU!" >_<
ms77462377 1 year ago 28
I get 41 every single time! lol
TheRatiniked 1 year ago
@TheRatiniked same here :D
sime0n4o 1 year ago
Learn what 'srand' does and re-do this video.
Jaso3333 1 year ago
This has been flagged as spam show
It gives me the EXACT same numbers every time! I have it set to give me three random numbers (1 - 10) and it gives me 2, 8, then 5 in that order! Please help me out!
Pokedude62 1 year ago
It gives me the EXACT same numbers every time! I have it set to give me three random numbers (1 - 10) and it gives me 2, 8, then 5 in that order! Please help me out!
Pokedude62 1 year ago
good example. thanks!
shruv 1 year ago
This doesn't really work as an dice since it gives the same numbers.
TaceoGamingTutorials 1 year ago
why do i get the same numbers as you?
thanks.
zuzgon 1 year ago
yo bucky
can you please explain this "%10" thing again? i dont really understand this...it devides the random number but why it always is the number that "smooth" then? like when i pick 5/10 or so it wouldnt work or what?
eklok5000 1 year ago
@eklok5000 ups got it, sorry
eklok5000 1 year ago
hey!what happen with this site??wtf!
ikeuchirobby 1 year ago
why do i i get the exact same numbers every time?
mattyinthecaddy 1 year ago
omg i got 41 tooo :O
mattyinthecaddy 1 year ago
"oook, what I do, forgot a semi colon. That's embarrassing" LOL
Shorty20122012 1 year ago
Great tutorial :) keep doing :)
davidtamuna1 1 year ago
BobDoubelina 1 year ago
The versions of rand() and srand() in the Linux C Library
use the same random number generator as random() and sran
dom(), so the lower-order bits should be as random as the
higher-order bits. However, on older rand() implementa
tions, the lower-order bits are much less random than the
higher-order bits.
BobDoubelina 1 year ago
It's always 41!!!! random my ass xD
KingJulien234 1 year ago
Is that Dev - C++?
vjromeguy 1 year ago
@vjromeguy ya, it is. he says he has it in his first tutorial i think
theMacBoy11 1 year ago
I got the same numbers as you got, so if many computer get the same random numbers I think we can crack the code for the slot machines at the casino :)
scancool 1 year ago
Your name is greg... im a genius
temesgen54 1 year ago
it say rand is undeclared. :/
TheRiot121 1 year ago
when i choose only rand() and it gives always 41!
How do i fix it?
R4TH4CK3R 1 year ago
@R4TH4CK3R Before you make your first call to rand(), seed the random number generator using the srand function.
NerdInHisShoe 1 year ago
Comment removed
01HiZZo 1 year ago
@R4TH4CK3R go watch the next video, he explains it there.
01HiZZo 1 year ago
The numbers aren't random. I'd get the same results every time I compiled and ran, and I got the same results as Bucky, too.
HHBones 1 year ago
I got the same exact results as you, Bucky...
crazynoob11 1 year ago
@crazynoob11 Ohhh, just watched the 13th vid. K nvm.
crazynoob11 1 year ago
Function rand should have a prototype error. ~_~
netchumpc002 1 year ago
What a nice idea to teach us how to get random numbers on the screen. :-)
Your tutorials are my favourite tutorials on YouTube as well as HTML and XHTML-tutorials by an other chap.
Anywawy, the point is, your C Programming Tutorial-Series is very good,and I've actually learned C from your tutorials. Nice work.
PersonalComputer96 1 year ago
BORING! The numbers are the same
Ashocka1 1 year ago
Great Tut.
TheDarkSagan 1 year ago
Thx for this tutorial. Please how can i use a "for statement" to generate EVEN number between 1 and 10? Many thx
Mcbere 1 year ago
@Mcbere do a random number between 1 and 5, then multiply by 2?
Rallybus 1 year ago
@Rallybus Genius.
crazynoob11 1 year ago
Comment removed
KPop021 1 year ago
why is it always 41 is the first random number you get here..?? thats why when divided by 10., u got the remainder 1., and when by 6 and added 1 u got 6..??
sidd9980 1 year ago
@sidd9980 its the 'seed'.
a random number generated from a seed to create a number depending on what the seed is.
If you had a seen based on RTC(real time clock) and wanted random numbers between 0 and ten everytime u press the space bar, if u pressed it 1 time per second it would have 0,1,2,3,4,5,6,7,8,9,10,0,1,2,3,4,5,6,7,89,10 ect.
in this case, the seed is based on the code or number of lines (i think)
TheSecOrg 1 year ago
this is mildly interesting, the numbers that came up on my system are the same as yours.
jackkniphe 1 year ago
@jackkniphe I got 41 as well. Odd.
ValosTaonas 1 year ago
for those who are having a problem with an undeclared rand function just add #include <stdlib.h> so that the rand function can be recognized. thanks bucky ive learned this from you on lesson 1 :)
sOulrOckmelO 1 year ago 25
@sOulrOckmelO Thanks! It worked! I had that problem and this fixed it.
ValosTaonas 1 year ago
@sOulrOckmelO thankyou so much i
Hattlen 10 months ago
@sOulrOckmelO thankyou so much
Hattlen 10 months ago
@sOulrOckmelO i didnt See Stdlib.h in the leSSon one :O tell me i need to learn EVEREYH TING :D
Abraham2nd 5 months ago
hey neuboSton hao can i make a program vith a neat interface inStead of the cmd ? can u plz make tut or can anyone tell me plz i tink i need viSual baSic?? or Something? i need a vid LOL
Abraham2nd 5 months ago
after i compile it wont work can you help?
gumgum7230 1 year ago
I can't see any differences between my code and yours, and I'm using Dev-C++ but when I go to compile and run this code it gives me the message, " 'rand' undeclared " and so it doesn't work.
bluecobra95 1 year ago
i love you man. im currently taking logic and design.. and this is perfectly making sense. i feel genius.
boniturs 1 year ago
I get very large numbers from the random function, 64-bit gcc on fedora 12
ender2070 1 year ago
What .whatever did you save it as? My text editing program, textwrangler, uses that to decide what to run it as, because it handles pythons and phps and everything, so I have to know the .ending......
Killjoyxtrm 1 year ago
Comment removed
ipromisenot 1 year ago
not random
felipe200794 1 year ago
@XDretsamtovip
You need to add a function: srand(time(NULL));
diegoramos27 2 years ago
cold i do this to get random characters
Digadogup 2 years ago
yes you could but i'm not sure how to do it. characters do have number values in the computer. maybe that's a tip.
x11115 1 year ago
@Digadogup yes.
int variable = 67;
printf("%c", variable);
or typecast it
char(variable)
or
(char)variable
0121ryanh117 1 year ago
It does the same thing every time :-/
khjuiproductions 2 years ago 20
@khjuiproductions you haven't seen next tutorial, have you'
EC2538 1 year ago
@khjuiproductions thnx bro!
monguiller 9 months ago
@khjuiproductions thnx bro! dude did you know all the list of #include so i can just put it all every i make a script .. if you know all .. give it to me .. thanks you ^^
monguiller 9 months ago
can some give all the #include!? so, i just put it all every i make a script ,,
monguiller 9 months ago
@monguiller what do you mean?
bondservant4Him 9 months ago
@khjuiproductions Watch the next video :)
edvardiant 7 months ago
@khjuiproductions
See next tutorial! Bucky know that problem and he explain it in next tutorial.
WinXPproff 6 months ago
To create a random number, just watch the next video instead of spamming this vid's comments!
nxter2009 2 years ago
These are not proper random numbers. I get the exact same numbers as you and the exact same numbers each time it's run. Proper random numbers should be different each time.
icekat83 2 years ago
You cannot truly get random numbers from a computer. The best way to get random numbers is radio static from the atmosphere.
BaneOfFreeSpeech 2 years ago
Same here. That's so weird.
icekat83 2 years ago
omg i love you!!!!!
persianqu33n 2 years ago
not exactly a function for choosing lotto numbers, if you want a different number each time...
srmru1 2 years ago
Easiest random number generator: return 4;
fallrite 2 years ago
my random number was 41 too
sporefergieboy10 2 years ago
I am having an error ...it says that rand its undeclared plz help
pip0120 2 years ago
How did you declare the random number?
Aggregate02 2 years ago
Please, you don't have any idea what you're doing. Just look at your code formatting.
Quote : "you can do a bunch of different math operators". Yeah...
fingerprint211b 2 years ago
Why if I have it print only one random number, it always gives me the same number?
TheThissucks123 2 years ago
the random function isn't exactly "random". The function is exactly what it says, a "function". Which means that its going to go on a specific path of operations. Just something to keep in mind.
PS when you do this code he has up here, your going to get the EXACT SAME numbers as him :D
MiChEaLjUnG 2 years ago
after watching 3 times i was able to understand it
naving8 2 years ago
did this not work for anyone? mine had an error saying
9 C:\Programming\Random number functions.cpp `rand' undeclared (first use this function)
can anyone give me a hand? i have exactly the same as bucky on my screen
omgwtfpwned00 2 years ago 2
Yeah. I am also having the same problem with Dev C++. It says 'rand' undeclared firs use ... error. How did you correct this?
Undurkhaan 2 years ago
When doing this in Dev-C++ I always get the same "random numbers", could someone please explain why?
blaergh123 2 years ago
Thank you.
aqa722 2 years ago
ZZzz
azitaulava 2 years ago
uhh... i ran the program and i got the same results as you
KingOfAnama 2 years ago
i should message you my homework and you should code it for me,lol
newmanto03 2 years ago
"the fence post problem"
MurderedAnimalFood 2 years ago
Max num is likely 65,535.
MurderedAnimalFood 2 years ago
Thats an incorrect description of what the function does. It doesn't provide a random number, but a pseudo-random number. Remember that functions are just code, that perform instructions. Nothing random about that.. just a complex algorithm.
MurderedAnimalFood 2 years ago 2
Yawn.
mahcuz 2 years ago
/agree
bikarida 2 years ago
this is weird but i got it
rubioelpayazo 2 years ago
well, i'd say the odds of getting caught in a group of 60 people is 1 in 60...
gescoboza 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
can i use it to find out what the odds of my getting caught at a party with 60 people there
aliopez 2 years ago
i can t find your firts tutorial about c
JLPZ76 2 years ago
for some reason after i put the rand part in it wont compile help please
tyleriscool45 2 years ago 3
Can I please see your compiler error, Tyleriscool45?
TonyTheJavaGuru 2 years ago
message me ur code and ill fix it for ya
thenewboston 2 years ago
just sent it check your messages
tyleriscool45 2 years ago
@thenewboston I did exact the same and compiler gave me 41 ;0
givi2278 1 year ago
Hey dude what happened to the site this is your moderator P0w3r0f1.
p0w3r0r1 2 years ago
dumb spammers kept postin and i got in trouble cuz they were posting adult content and i had adsense on my site
thenewboston 2 years ago
@thenewboston how do we contact u plz tell
sainathi34 1 year ago