What is the difference between using time(0) and time(NULL)? And when declaring a variable to set time() to, suck as s, what is the difference between declaring it int s; and time_t s; ?
@Listn2CKY that's because if you divide a number by itself, in this case 6/6, it gives you a remainder of 0, which is a whole number (no decimals).. so 6/5 is remainder. 1, 6/4, 6/3,6/2, 6/1 are remainders 2, 3, 4, and 5 respectively. so the only numbers you can get are 0-5.
@1Razerkey It can still be predicted, in a sense. Try making an srand time generator, and running it every few seconds. The value changes in predictable amounts, unless you're using another function, like the modulus.
Is there anyway to have one number show up more than another? Let's say I wanted either 1 or 2 to show up but I want 1 to show up 55% of the time and 2 to show up 45% of the time. How could I go about doing that?
Check out this random number function I made. If you ever need a random number, you can say what number range you want to do. like RandNum(10,50) Will generate numbers from 10 to 50.
Why do we need to put 0 in the parameter for the time function, when that's all you would ever put there? That doesn't really make it a "parameter"...
If I am not mistaken , rand() is actually a recursive function.
It uses the result it last generated, to generate the next.
So wha t srand() actually does, is make it not take the last result, but the number provided by it, or maybe one generated after a few calculations from the number you gave in srand().
That's what I was told at my university at least, I'm in my first year in ECE department, so I am not the most reliable source :D
@AlmightyMatthew no dude thats another thing, u need to use the time function that takes in systems time and plugs it in the rand function, so everytime it generates a different number :)
the thing i wus talking about is universal real life thing, if u in real life picked up random numbers by hand same thing wud happen!
Thank you bucky for ending my endless search in mircosoft for random generator that didn't use C or Visual Basic... And it is a lot less complexed....
@AleksGSquadron Yes, but that is more like YOU need to script it. Basically make a for/while loop that will do the random, then save that number into an array, then move on to the next one, it will random, then it will check the array against the random number to see if they match, if they do, it will run again and retry until it gets a trully different value. May take you a little time but it will finally work out in the end.
Gotta question, you say the time of your computer is changing every second. I understand that. Now, let's say you have the program create a long list of random numbers so that it takes more than a second to compute them all. Let's just say 1000 numbers, at 500 numbers a second to make it easy. Will the first half and the second half be on two different algorithms since the time has changed?
@ResidentBiscuit Nope, it'll all be on the same algorithm. You can think of it like this: imagine the huge list that repeatedly calling rand() generates. When you call srand() with a number, it generates a different 'list' of numbers for rand() to 'read' from (based on some funky algorithm). Since you have defined this list once, it won't change, regardless of how many numbers you generate after that. Hope this helps!
@ResidentBiscuit It depends on if you include srand(time(0)) in the loop. If you don't then the seed will not change and it will use the same algorithm.
@Chriscs7 I'm not much farther then you then. I didn't finish ALL of them yet, what I meant is that I love finishing each individual tutorial. I really doubt it though, as the last one, 73 or something, says that Bucky'll soon start making tutorials on gaming. You might have to learn another programming language also that allows you to draw 3d objects, then link those 3d objects with c++. I'm no expert on the subject, so don't go screaming liar if anything I said happens to be wrong.
basically those programs are setup for people that seem to make the same mistakes a lot about forgetting to add an include so if it sees an error it adds the include for you but you wouldn't know that because it was done while you built it so it never adds it to your code instead it adds to the end result but by doing that it also makes it easier to screw up if you want to use a different program its best to have it right in the first place
@Mahiz96 Hmm.. tha's strange. Your code doesn't work on code::blocks, but i tried it on dev-C++ and there it works smoothly. Now I'm really confused... You don't even need ctime library for seeding a number. Would anyone explain it to me how's that possible? xD
Why not just give me a list of header files, and I can copy past them all in every time I start making a new program? Even if I dont need some of them.
I used Dev C++ to do this program but when I click to compile and debugg (it finish without any errors) just a black thing appear and disappear. I'm using win7 and I'm new to this, help please thanks.
If you want "actual" random numbers you can seed with the time by including ctime and then calling srand(time(NULL)) at the beginning of your program. This will give random numbers depending on when the program is run.
The reason everyone gets 41 is because it's random number seed (What the random number comes from.) is probably the amount of memory used and if you've done exactly what Bucky has done, it's going to be 41.
@Gabrielregalado2 all c++ need main and header files, header files is a library with function so your program knows what its doing, and all programs need main because its where the programming takes place watch Buckys c++ from the beginning
My programming teacher uses a file with a "file.h" extension , a file with "file.cpp" extension and a normal "main.cpp" file , she makes the different tabs work together to a single file , do you know a webpage/Video explaining how the different files work?
compared tot he ones of thenewboston of course ...
ecence123 17 hours ago
seriously all my random numbers are exactelly the same!
ecence123 17 hours ago
Comment removed
warriorsfan12331 1 day ago
What is the difference between using time(0) and time(NULL)? And when declaring a variable to set time() to, suck as s, what is the difference between declaring it int s; and time_t s; ?
dylanXdisaster 4 days ago in playlist C++ Programming Tutorials Playlist
the time() function returns the number of seconds from January 1, 1970
dylanXdisaster 4 days ago in playlist C++ Programming Tutorials Playlist
Thanks Bucky. I love the way you explain everything. You're awesome dude!
Gervisj 6 days ago
Arnt we just biocomputers?
funkydiddykong 1 week ago
@funkydiddykong arent computers just techbrains?
MisterJimLee 1 week ago in playlist C++ Programming Tutorials (thenewboston)
I hate ayn rand
Regginyaga 2 weeks ago
How do you stop console close immediately after running the program?
finster796 2 weeks ago in playlist C++ Programming Tutorials Playlist
I'm not the best at maths but could someone please explain the logic for 'you can't divide any thing by six with remainder of 6'.
Listn2CKY 2 weeks ago in playlist C++
@Listn2CKY that's because if you divide a number by itself, in this case 6/6, it gives you a remainder of 0, which is a whole number (no decimals).. so 6/5 is remainder. 1, 6/4, 6/3,6/2, 6/1 are remainders 2, 3, 4, and 5 respectively. so the only numbers you can get are 0-5.
brawl313 2 weeks ago
@brawl313
Thank you. It seems obvious now but I couldn't think.
Listn2CKY 2 weeks ago
umm.. bucky using the srand time thing it is truly random, since the seed is never the same.
1Razerkey 4 weeks ago in playlist Buckys C++ Programming Tutorials Playlist
@1Razerkey It can still be predicted, in a sense. Try making an srand time generator, and running it every few seconds. The value changes in predictable amounts, unless you're using another function, like the modulus.
Geeksunitedvideos 1 week ago
anyone else having difficulty getting code::blocks to run this?
KristopherSw 1 month ago
@KristopherSw you most likely made an infinite loop.
michaeljs1995 3 weeks ago in playlist C++ Programming Tutorials Playlist
how can i set a variable to a random number? Like if i wanted to make a game where the user rolls a dice and if the answer is 2 then they win.
johnthesman 1 month ago in playlist More videos from thenewboston
@johnthesman
int diceRoll = 1+rand()%6;
ben1996123 4 weeks ago
time(0) is giving me the same number everytime :/
anyone knows why?
musvcHack4cent 1 month ago
Is there anyway to have one number show up more than another? Let's say I wanted either 1 or 2 to show up but I want 1 to show up 55% of the time and 2 to show up 45% of the time. How could I go about doing that?
Malkfleursby 1 month ago
@Malkfleursby
num << rand()%100;
If num is between 0 and 44, option a
If num is between 45 and 99, option b.
Jamessssy 1 month ago
And I used the loops variable to make it generate a new number EVERY time you use the function, rather than every second.
TheXRealXBrapp 1 month ago in playlist Buckys C++ Programming Tutorials Playlist
This has been flagged as spam show
Check out this random number function I made. If you ever need a random number, you can say what number range you want to do. like RandNum(10,50) Will generate numbers from 10 to 50.
int RandNum(int BetweenA,int BetweenB){
int loops;
loops++;
if (loops>9999){loops=0;}
srand(time(0)*loops/150);
int difference=BetweenB-BetweenA;
return (rand()%(difference+1)+(BetweenA));
}
TheXRealXBrapp 1 month ago in playlist Buckys C++ Programming Tutorials Playlist
Comment removed
TheXRealXBrapp 1 month ago in playlist Buckys C++ Programming Tutorials Playlist
Why do we need to put 0 in the parameter for the time function, when that's all you would ever put there? That doesn't really make it a "parameter"...
TheXRealXBrapp 1 month ago in playlist Buckys C++ Programming Tutorials Playlist
what are the chances that my first random result was 41 as well?
chaostroll1267 2 months ago in playlist Buckys C++ Programming Tutorials Playlist
@chaostroll1267 It has not been seeded yet, so it will always be the same. :-)
gudenaurock 2 months ago in playlist Buckys C++ Programming Tutorials Playlist
i love how no matter how short/long the tutorials are from bucky I always end up spending hours watching them [=
Silverbender188 2 months ago in playlist C++ Tutorials
if you just print out rand you always get 41.
MysticalRhythms 3 months ago in playlist Buckys C++ Programming Tutorials Playlist
I don't get it. I don't have cstdlib in my header, and rand still works.
2pimpinout 3 months ago
let's find the house of the disliker and burn it down!!!
MiloSx7 3 months ago
If I am not mistaken , rand() is actually a recursive function.
It uses the result it last generated, to generate the next.
So wha t srand() actually does, is make it not take the last result, but the number provided by it, or maybe one generated after a few calculations from the number you gave in srand().
That's what I was told at my university at least, I'm in my first year in ECE department, so I am not the most reliable source :D
Rigardoful 3 months ago in playlist Buckys C++ Programming Tutorials Playlist
Comment removed
mattdoughboy 4 months ago in playlist Buckys C++ Programming Tutorials Playlist
Comment removed
ShaneCopseysTV 4 months ago
Comment removed
ShaneCopseysTV 4 months ago
Wow, why in gods name is it so difficult for my professor to explain random numbers while bucky can do it in a few minutes?
Inuzadrop 4 months ago
@Inuzadrop Because Bucky explains it much more simpler way than your professor... :D Bucky explain this as he has learned itself... :)
Maketsup 3 months ago
That's hot.
trueredexe 4 months ago
This is by far the best explaination of rand() I've ever seen, seriously bucky very very well done man.
onelerv1 4 months ago in playlist Buckys C++ Programming Tutorials Playlist
in my language rand = prostitute !!!! :P
maharshi1893 4 months ago 24
FUN FACT: if u generate random numbers between 1 and 365, about only 30 of em, its almost sure that u will find 2 numbers same!
(its called birthday problem from probability)
awesome tut bucky! :)
nextblain 5 months ago
@nextblain so ... my rand isnt broken if its showing the same numbers as in the video?
AlmightyMatthew 3 months ago
@AlmightyMatthew and lol if waited for exactly 1s in the video id know
AlmightyMatthew 3 months ago
@AlmightyMatthew no dude thats another thing, u need to use the time function that takes in systems time and plugs it in the rand function, so everytime it generates a different number :)
the thing i wus talking about is universal real life thing, if u in real life picked up random numbers by hand same thing wud happen!
nextblain 3 months ago
I will never buy another programming text book ever again!
TRX2015 5 months ago
Comment removed
TRX2015 5 months ago
how do you get today's date with the ctime class? if that is possible ofc, I just assume it would be.
Ciemon 5 months ago
Thank you bucky for ending my endless search in mircosoft for random generator that didn't use C or Visual Basic... And it is a lot less complexed....
mylifeis2coolcmc2 5 months ago
The time you're referring to is Unix Time (Aka Epoch Time and POSIX Time).
NorthboundFox 5 months ago in playlist TheNewBoston - C++
why does the prog only return 21 numbers?
thebaronofmetal 5 months ago
timestamp! :D
bohterham 5 months ago in playlist TheNewBoston - C++
if anyone is struggling with rand(), and getting the correct range of numbers, use this:
x+(rand()%(y-(x-1))) gives a random number from x to y inclusive, eg:
5+(rand()%(10-(5-1))) generates a number 5, 6, 7, 8, 9 or 10.
ben1996123 6 months ago
thumbs up if you did cout << rand(); and got 41 as well
ben1996123 6 months ago 39
@ben1996123 It's because it isn't random per say.
1Razerkey 4 weeks ago in playlist Buckys C++ Programming Tutorials Playlist
How can i make a random generator for an array with 9 numbers (sudoku)
so the numbers cannot be repeated? Is there a command which saves the last
number choosen?
AleksGSquadron 6 months ago
@AleksGSquadron Yes, but that is more like YOU need to script it. Basically make a for/while loop that will do the random, then save that number into an array, then move on to the next one, it will random, then it will check the array against the random number to see if they match, if they do, it will run again and retry until it gets a trully different value. May take you a little time but it will finally work out in the end.
mylifeis2coolcmc2 5 months ago
Gotta question, you say the time of your computer is changing every second. I understand that. Now, let's say you have the program create a long list of random numbers so that it takes more than a second to compute them all. Let's just say 1000 numbers, at 500 numbers a second to make it easy. Will the first half and the second half be on two different algorithms since the time has changed?
ResidentBiscuit 6 months ago
Comment removed
PS3Ydf 6 months ago
This has been flagged as spam show
@ResidentBiscuit Nope, it'll all be on the same algorithm. You can think of it like this: imagine the huge list that repeatedly calling rand() generates. When you call srand() with a number, it generates a different 'list' of numbers for rand() to 'read' from (based on some funky algorithm). Since you have defined this list once, it won't change, regardless of how many numbers you generate after that. Hope this helps!
PS3Ydf 6 months ago
@ResidentBiscuit It depends on if you include srand(time(0)) in the loop. If you don't then the seed will not change and it will use the same algorithm.
anotherMakaron 6 months ago
@ResidentBiscuit
srand(time(unsigned));
that unsigned makes it deadly random, none can really predict it!!!!
AleksGSquadron 6 months ago
Comment removed
TrimakX 7 months ago
what kinda notepad are your writing on? im useing notepadd++ i cant get it to work >.<
Xwooterz 7 months ago
@Xwooterz This is Code Blocks.
DrQu4ntum 7 months ago
This has been flagged as spam show
couldn't you do srand(rand())?
lancerator08 7 months ago in playlist Buckys C++ Programming Tutorials
Comment removed
philohan95 7 months ago
the alg does not change at all...you just change the value that the alg uses ..great tutorials btw ^_^
Swamp1191 7 months ago
i also got 41 O.o
klakiti02 7 months ago
cout << time(0);
garfield234234234 8 months ago
so if i could hack my PC and stop my pc clock
would it generate the same number over and over again?
PorifersAnims 8 months ago
It's so much easier in Java...
ValenMedia 8 months ago
I want to scream my love for his tutorials but that'd make me look like some sort of fan boy, finishing his tutorials just fills me with... bliss.
TakingItCasual 9 months ago 2
@TakingItCasual Hi ,can you make a 3D game after finishing his tutorials,I am at tut 40 alredy
Chriscs7 8 months ago
@Chriscs7 I'm not much farther then you then. I didn't finish ALL of them yet, what I meant is that I love finishing each individual tutorial. I really doubt it though, as the last one, 73 or something, says that Bucky'll soon start making tutorials on gaming. You might have to learn another programming language also that allows you to draw 3d objects, then link those 3d objects with c++. I'm no expert on the subject, so don't go screaming liar if anything I said happens to be wrong.
TakingItCasual 8 months ago
i got the same numbers as result....it seems random be not so random, why that?
kota1102 9 months ago
@kota1102 You didn't watch the whole video when you commented this did you?
CCNezin1 9 months ago
@CCNezin1 :) indeed
kota1102 9 months ago
basically those programs are setup for people that seem to make the same mistakes a lot about forgetting to add an include so if it sees an error it adds the include for you but you wouldn't know that because it was done while you built it so it never adds it to your code instead it adds to the end result but by doing that it also makes it easier to screw up if you want to use a different program its best to have it right in the first place
nanobain1 9 months ago
@Mahiz96 Hmm.. tha's strange. Your code doesn't work on code::blocks, but i tried it on dev-C++ and there it works smoothly. Now I'm really confused... You don't even need ctime library for seeding a number. Would anyone explain it to me how's that possible? xD
WhanaSeeWhatYouGot 9 months ago
@Mahiz96 can i see the code pls? i really want to, cuz i doubt that that's possible, of course i can be mistaking, but still i'd like to view it.
WhanaSeeWhatYouGot 9 months ago
Why not just give me a list of header files, and I can copy past them all in every time I start making a new program? Even if I dont need some of them.
olivebates 9 months ago
@Mahiz96 fail comment of the day
u need to include cstdlib library cuz it contains information on what rand() function is... It's the same with iostream and cout.
P.S. if u don't include it u will certainly get an error which says that rand was not declared.
WhanaSeeWhatYouGot 9 months ago
Actually humans aren't random either :P A human's "random" is also influenced by whats in the area , what just happened ,... :P
D4L33tz0r 9 months ago
????? omg i did the xact same thing and my compiler printed out 41 just like urs----are u sure its completely random and not in a pattern o.O
lifetycoon101 9 months ago
its always 41!!!!
coder1997 10 months ago
I used Dev C++ to do this program but when I click to compile and debugg (it finish without any errors) just a black thing appear and disappear. I'm using win7 and I'm new to this, help please thanks.
Raitoification 10 months ago
More than 1293833966 seconds have gone by since 1970
ChasLough 10 months ago
Question: How to take random element from array. Example: I hawe 10 names and i want to get 1 random name from that array.
bulve 10 months ago
@bulve one thing you can try is to give each name a corresonding number, like
Name 1 =1
Name 2 =2 and so forth
then depending on wich number it randoms it pulls up the numbers corresponding name. hope this helps
MrHankkey93 10 months ago
"To tell you the truth, computers aren't humans" ... aww :(
erniebondy123 10 months ago
Its ALWAYS 41!
broadude 10 months ago
Here's a random number generator that works well with Visual C++
In your compiler( Dev C++, I think) srand(time(NULL)) gives a random number still. In Visual C++, it's doing a random number per second, so do this
srand(time(NULL)+rand());
That will generate a different number each time, but in Visual C++, and you won't have to wait every second until you get a new randomized number.
winPGRM 10 months ago
you also get 41 ?
wonderbug10 10 months ago
If you want "actual" random numbers you can seed with the time by including ctime and then calling srand(time(NULL)) at the beginning of your program. This will give random numbers depending on when the program is run.
Fjerdue 10 months ago
Comment removed
Fjerdue 10 months ago
Bucky is Chuck Norris's programmer
blubberwalrus 10 months ago 79
@blubberwalrus Correction: Bucky is Chuck Norris' programmer.
nickvth2009 10 months ago
@blubberwalrus Screw that Bucky friggin' programmed Chuck Norris lol
foofightin33 9 months ago
This has been flagged as spam show
@blubberwalrus yous said: Bucky is Chuck Norris's programmer True hes awesome
yiopply 9 months ago
Hey bucky, why don't you post a video on the different data types, as in int vs short int vs long int vs unsigned int vs etc ?
wolterh6 10 months ago
The reason everyone gets 41 is because it's random number seed (What the random number comes from.) is probably the amount of memory used and if you've done exactly what Bucky has done, it's going to be 41.
mangopearandapples 10 months ago
I love you
pjevola20081980 10 months ago
could u make a video about arrays? including 2d?
Peop7 10 months ago
can you make a video on classes c++ please , how do you work with a main and header file? =(
Gabrielregalado2 10 months ago
@Gabrielregalado2 all c++ need main and header files, header files is a library with function so your program knows what its doing, and all programs need main because its where the programming takes place watch Buckys c++ from the beginning
Peop7 10 months ago
@Peop7
My programming teacher uses a file with a "file.h" extension , a file with "file.cpp" extension and a normal "main.cpp" file , she makes the different tabs work together to a single file , do you know a webpage/Video explaining how the different files work?
Gabrielregalado2 10 months ago
yeah you got 41 the last two times too
Wiikiller102 10 months ago
u are getting better and better
MaracTheCracker 10 months ago
Why is the filename Watermelon? lol
BrixXSM 10 months ago 12
@BrixXSM haha i name most of my files and classes foods. mostly whatever im in the mood for when i make the file
thenewboston 10 months ago 52
@thenewboston You're in the mood for Tuna most of the time ;)
sas1ni69 10 months ago
Hmm, shouldn't the video's name be something along the lines of "Bucky's C++ Programming Tutorials - 27 - random numbers", rather than "cpp27"?
nonetheless, excellent tutorial :)
Xemerau 10 months ago