what are you even trying to achieve? I guess it is to have a RNG which returns numbers between between min and max. the way to do it correctly would be new_value = min + rand() % (max-min), as the current implementation doesn't do this correctly. the loop is entirely unnecessary - there is no need to ensure that new value is different than previous, as random numbers can repeat. I guess your're are problem was that you were getting the same number repeated all the time, but this is because your're are using srand() wrong. it re-seeds the global RNG, so calling it should not be the responsibility of myrand() but of the calling program. also, unrelated to speed: returning 1 is weird here because 1 is a valid return value of an RNG, I'd rather use errno to indicate wrong arguments. a rough solution would look like this (warning: I did not compile it):
int myrand(int min, int max) { static short seeded = 0; //assuming your're are using a standard from before bool type if(!seeded){ srand(time(NULL)); seeded = 1; } if(min > max){ errno = EINVAL; return 0; } return min + (rand()%(max-min)); }
int main() { for(register char c = 0; c < 5; c++) { printf("%c\t", myrand(33, 126)); }
int main() { int next = time(NULL); char buffer[11]; for(char c = 0; c < 5; c++) { next = next * 1103515245 + 12345; buffer[c*2] = (unsigned char)33 + (unsigned int)(next/65536) % (127-33); buffer[c*2+1] = '\t'; } buffer[10] = 0; puts(buffer); return 0; }
Name:
Anonymous2019-09-11 17:39
Why so much boilerplate and hard work...
$ python3 >>> import random >>> print('\t'.join(str(random.randint(33, 127)) for i in range(5))) 118 76 88 65 82
Problem solved.
Name:
Anonymous2019-09-11 22:26
with Ada.Numerics.Discrete_Random; with Ada.Text_IO; use Ada.Text_IO; procedure Random is subtype Printable_Character is Character range Character'Val(33) .. Character'Val(126); package Random_Printable_Character is new Ada.Numerics.Discrete_Random(Printable_Character); use Random_Printable_Character; G : Generator; Current, Previous : Character := Character'Val(0); function Unique return Character is begin loop Current := Random(G); exit when Current /= Previous; end loop; Previous := Current; return Current; end Unique; begin Reset(G); for I in 1..5 loop Put(Unique); end loop; New_Line; end Random;
Name:
Anonymous2019-09-11 22:56
>>14 More boilerplate, noise and unproductive crap. As bad as C but uglier.
The best programming language is English. The day we can make computers execute busines logic, like "Print five random ints from 33 to 127, tab-separated", you all can keep portable assembly, Sepples, Javur, C-octothorpe and the like to yourselves, I have business to do and my time is far more expensive than execution time jerk-offs.
In the mean time I'm content with the FIOC and similar dynamic, high-level, multi-paradigm languages where at least I can get close enough to English and keep the ratio of business logic lines to boilerplate shit quite high. And also stay out of OOP, which requires a lot of that shit too with its classes, interfaces, ``design patterns'' and similar workarounds to using fucking λ-expressions.
Print five random ints from 33 to 127, tab-separated
this is still basically assembly. you're still telling the computer how instead of what. it's just a step above:
(loop for x = (random 128) unless (< x 33) collect x into numbers until (>= (length numbers) 5) finally (return (format t "~{~A~T~}" numbers)))
which is a thin wrapper over primitive operations despite reading like a sentence
"make a game that's a combination of pong and space invaders" -- that's when programmers have to start sucking cock for a living Edited on 11/09/2019 23:35.
>>15
Print five random ints from 33 to 127, tab-separated
this is still basically assembly. you're still telling the computer how instead of what. it's just a step above:
(loop for x = (random 128) unless (< x 33) collect x into numbers
until (> (length numbers) 5)↵
finally (return (format t "~{~A~T~}" numbers)))
until (>= (length numbers) 5)↵
finally (return (format t "~{~A~T~}" numbers)))
which is a thin wrapper over primitive operations despite reading like a sentence
"make a game that's a combination of pong and space invaders"
-- that's when programmers have to start sucking cock for a living
>>22 Second that. Haskell has lazy evaluation, which is a very efficient way to compute thing. A call to OP's myrand wont be evaluated until it's value is actually needed, or it could be evaluated in a separate thread, while main thread does its own shit.
Name:
Anonymous2019-09-12 11:40
Can we see the ideal Haskell solution?
Name:
Anonymous2019-09-12 13:09
>>24 I don't know much about Haskell but it would probably look like "5 uniq rnd(33,126) print "
The requirement clearly states 'How do I make this faster?'. Learn to read you Execution time is **the** requirement.
This is /prog/, you dolt. We never do whatever we're supposed to. Edited on 12/09/2019 23:01.
>>29
The requirement clearly states 'How do I make this faster?'. Learn to read you Execution time is **the** requirement.
This is /prog/ you idiot. We never do whatever we're supposed to.
This is /prog/, you dolt. We never do whatever we're supposed to.
Name:
Anonymous2019-09-15 18:49
In HolyC you could store that entire string within a cpu register avoiding costly memory instructions.
Performance increase of ~60x.
In C you could avoid memory access thusly: -
#include <time.h> #include <stdio.h>
int main() { register unsigned int next = time(NULL);
for(register int c = 0; c < 5; c++) { next = next * 1103515245; next = next + 12345; next = next / 65536; next = next % 94; next = next + 33; putchar(next); putchar('\t'); } return 0; }
Name:
Anonymous2019-09-15 19:00
>>31 That is what Symta does. But Symta does a lot of text processing and most strings are small enough to fit into 64-bit registers.