20

TL;DR - Girlfriend wanted to learn coding, I might have scared her off.

Today, my girlfriend said she wants to learn coding.
Me: why?
She: well, all these data science lectures are recommending Python and R.
Me: Ok. But, are you interested in coding?
She: No, but I think I have to learn.
Me: Hmm.. coding requires a clear thought process, and we should tell the computer exactly what needs to be done.
She: I think I can do that.
Me: Okay... then tell the computer to think and give a random number between 1 to 10.
She: I will use that randint function. (She has basic knowledge in C)
Me: Nope. You write your own logic to make the computer think.
She: What do you mean?
Me: If I were you... Since it is just a single digit number.. I would capture the current time and would send the last digit of milliseconds @current time.
She: Oh yeah, that's cool. Understood! I will try...
" " "
We both work in same office.. so, we meet up for lunch

" " "

I didn't ask about it, but she started,
She: Hmm, I thought about it, but I was not able to think of any solution. May be its not my cup of tea.

I felt bad for scaring her off... :(

Anyway, what are some other simple methods to generate random numbers like OTPs. I am interested in simple logics, which you have thought of..not the Genius algorithms we have in predefined libraries.

Comments
  • 28
    Pfft. What kind of beginner uses a random integer function for generating random numbers? True C programmers just access arrays out of bounds to get random data.
  • 6
    Reading data from /dev/urandom and converting it to int or long maybe?
  • 5
    @NonImportant- <-- that would work too!! Malloc an array and grab some bytes from it w/o nulling it first!
  • 3
    @NonImportant- Haha, that's a good idea!
    Btw, she is !dev.
  • 3
    @netikras Correct me if I'm wrong. That would again be like using a predefined algorithm, right?
  • 3
    @PokerJack Accessing out of bounds arrays is actually very unsafe (and very dangerous in terms of security), instead just use C's built in random function (my original post is sarcasm, but the method does very well work).
  • 3
    @PokerJack yes. But you never know what data lies in the freshly malloc'ed block :)

    as for urandom - you asked for simple, not secure :)
  • 5
    btw, not to bother the offtopic thing about random generators you have going...
    The girlfriend approach to coding is totaly legit. She has a problem to solve, and decided to use programimg to solve it. Help her solve it. If she finds the tool, and road to.the solution interesting/fun - She might become a real coder. If she gets to the solution, but never wants to code again, then too bad. If she gives up im the middle.... nm then. But she needs to experience solving a "real" problem by herself - not a made up one, that no one will use, care about, and that someone else already solved.
  • 3
    @magicMirror off-topic? The OP has literally asked for it.
  • 6
    @netikras Actually, malloc will give you zeroed memory if your process allocates the memory behind that for the first time. Anything else would be a major security hole in the OS.

    Upon subsequent free/malloc cycles, the OS can drop the zeroing if it's a page that you already had, in which case your own process' data from previous usage will be inside.
  • 3
    @Fast-Nop you're right, I completely forgot this has been implemented later on :)
  • 3
    I haven't seen a single correct answer. Mostly because @PokerJack asked for a number between 1 and 10, not between 0 and 9 😋

    Suggestion: (PID mod 10) + 1

    But yeah, I think you scared her a little bit.
  • 2
    Wouldn’t this work?

    int a ;
    printf(a%10) ;
  • 3
    @neriald if a is a global variable, it will be initialised to 0 automatically. If it's a local, it's undefined behaviour, and the compiler can discard that.
  • 2
    @Fast-Nop I was thinking on the stack non global. Also printing a would’ve printed random number so I thought modulo could also work but I guess you’re right.
  • 2
    @neriald - I think it would work only if I want to generate one number per each instance of program run.
    However, if I want a random number generated like 5 times in same instance of program, then it would return the same number all 5 times.
  • 2
    @PokerJack well, if we assume that arithmetic operations work on uninitialized variables it would take one more variable and a for loop to generate that.

    Int a;
    Int b;
    For(i <5){
    Print(a%10);
    A=a*b;
    }
  • 1
    @neriald Hmm, at a glance I thought this would work. Now I think if one of them is even number, doesn't it always return an even number?
    I mean instead of 0 to 9, doesn't it return only 02468?
  • 3
    For generating OTP I usually do

    Math.floor(100000 + Math.random() * 900000);
  • 3
    @PokerJack nice catch =) tho it can be fixed with an if statement, it already became too long and ugly for such a simple task
  • 6
    Not to be condescending or anything but you were kind of a dick to her...
  • 7
    I know that you want to explain her "the real programming" but that's not how beginners learn coding.
  • 2
  • 1
    @netikras actually, it might fail. Reading from unallocated memory can be restricted by the OS, the memory can be nulled. I think there was a bug in one of BSDs or related to SSL somehow caused by this.
  • 1
    Yeah that was kinda a dick move. To me it comes off more like 'I'm really good at thinking', than 'let me show you what we do'.
  • 0
    Easy :) Just get the last digit of Pi number)
Add Comment