Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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.
-
@NonImportant- <-- that would work too!! Malloc an array and grab some bytes from it w/o nulling it first!
-
PokerJack535y@netikras Correct me if I'm wrong. That would again be like using a predefined algorithm, right?
-
@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).
-
@PokerJack yes. But you never know what data lies in the freshly malloc'ed block :)
as for urandom - you asked for simple, not secure :) -
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. -
@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. -
Hel8y3375yI 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. -
@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.
-
neriald14525y@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.
-
PokerJack535y@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. -
neriald14525y@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;
} -
PokerJack535y@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? -
neriald14525y@PokerJack nice catch =) tho it can be fixed with an if statement, it already became too long and ugly for such a simple task
-
I know that you want to explain her "the real programming" but that's not how beginners learn coding.
-
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'.
Related Rants
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.
random
random numbers
coding
girlfriend