Added: 10 months ago
From: thenewboston
Views: 21,954
Sort by time | Sort by thread (beta)

Link to this comment:

Share to:

All Comments (117)

Sign In or Sign Up now to post a comment!
  • compared tot he ones of thenewboston of course ...

  • seriously all my random numbers are exactelly the same!

  • Comment removed

  • 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; ?

  • the time() function returns the number of seconds from January 1, 1970

  • Thanks Bucky. I love the way you explain everything. You're awesome dude!

  • Arnt we just biocomputers?

  • @funkydiddykong arent computers just techbrains?

  • I hate ayn rand

  • How do you stop console close immediately after running the program?

  • 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 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

    Thank you. It seems obvious now but I couldn't think.

  • umm.. bucky using the srand time thing it is truly random, since the seed is never the same.

  • @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.

  • anyone else having difficulty getting code::blocks to run this?

  • @KristopherSw you most likely made an infinite loop.

  • 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

    int diceRoll = 1+rand()%6;

  • time(0) is giving me the same number everytime :/

    anyone knows why?

  • 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

    num << rand()%100;

    If num is between 0 and 44, option a

    If num is between 45 and 99, option b.

  • And I used the loops variable to make it generate a new number EVERY time you use the function, rather than every second.

  • Comment removed

  • 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"...

  • what are the chances that my first random result was 41 as well?

  • @chaostroll1267 It has not been seeded yet, so it will always be the same. :-)

  • i love how no matter how short/long the tutorials are from bucky I always end up spending hours watching them [=

  • if you just print out rand you always get 41.

  • I don't get it. I don't have cstdlib in my header, and rand still works.

  • let's find the house of the disliker and burn it down!!!

  • 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

  • Comment removed

  • Comment removed

  • Comment removed

  • 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 Because Bucky explains it much more simpler way than your professor... :D Bucky explain this as he has learned itself... :)

  • That's hot.

  • This is by far the best explaination of rand() I've ever seen, seriously bucky very very well done man.

  • in my language rand = prostitute !!!! :P

  • 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 so ... my rand isnt broken if its showing the same numbers as in the video?

  • @AlmightyMatthew and lol if waited for exactly 1s in the video id know

  • @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!

  • I will never buy another programming text book ever again!

  • Comment removed

  • how do you get today's date with the ctime class? if that is possible ofc, I just assume it would be.

  • 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....

  • The time you're referring to is Unix Time (Aka Epoch Time and POSIX Time).

  • why does the prog only return 21 numbers?

  • timestamp! :D

  • 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.

  • thumbs up if you did cout << rand(); and got 41 as well

  • @ben1996123 It's because it isn't random per say.

  • 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 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?

  • Comment removed

  • @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.

  • @ResidentBiscuit

    srand(time(unsigned));

    that unsigned makes it deadly random, none can really predict it!!!!

  • Comment removed

  • what kinda notepad are your writing on? im useing notepadd++ i cant get it to work >.<

  • @Xwooterz This is Code Blocks.

  • Comment removed

  • the alg does not change at all...you just change the value that the alg uses ..great tutorials btw ^_^

  • i also got 41 O.o

  • cout << time(0);

  • so if i could hack my PC and stop my pc clock

    would it generate the same number over and over again?

  • It's so much easier in Java...

  • 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 Hi ,can you make a 3D game after finishing his tutorials,I am at tut 40 alredy

  • @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.

  • i got the same numbers as result....it seems random be not so random, why that?

  • @kota1102 You didn't watch the whole video when you commented this did you?

  • @CCNezin1 :) indeed

  • 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

  • @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.

  • 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.

  • @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.

  • Actually humans aren't random either :P A human's "random" is also influenced by whats in the area , what just happened ,... :P

  • ????? 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

  • its always 41!!!!

  • 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.

  • More than 1293833966 seconds have gone by since 1970

  • 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 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

  • "To tell you the truth, computers aren't humans" ... aww :(

  • Its ALWAYS 41!

  • 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.

  • you also get 41 ?

  • 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.

  • Comment removed

  • Bucky is Chuck Norris's programmer

  • @blubberwalrus Correction: Bucky is Chuck Norris' programmer.

  • @blubberwalrus Screw that Bucky friggin' programmed Chuck Norris lol

  • 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 ? 

  • 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.

  • I love you

  • could u make a video about arrays? including 2d?

  • can you make a video on classes c++ please , how do you work with a main and header file? =(

  • @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

    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?

  • yeah you got 41 the last two times too

  • u are getting better and better

  • Why is the filename Watermelon? lol

  • @BrixXSM haha i name most of my files and classes foods. mostly whatever im in the mood for when i make the file

  • @thenewboston You're in the mood for Tuna most of the time ;)

  • 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 :)

Loading...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more