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

Pages: 1-

random

Name: Anonymous 2012-01-01 23:19

hey /prog/.

is there a random number generator algorithm/library that supports something like this: use this seed, give me next nth random number? obviously it shouldn't iterate n times. I don't care if it is not that random.

there is probably something very simple for this but I can't get my head around it.

Name: Anonymous 2012-01-01 23:40

So you're looking for a specific random number generator

Name: Anonymous 2012-01-02 0:00

int Random(){
    return 4; //Gauranteed to be random
}

Name: Anonymous 2012-01-02 0:04

>>1

would this work?


unsigned int random(unsigned int n) {
  return n*__REALLY_BIG_NUMBER_THAT_IS_RELATIVELY_PRIME_TO_MAX_INT__;
}

Name: Anonymous 2012-01-02 0:35

>>3
a little more random please
>>4
I don't think so

So in the end I will use the seed iterate through random values n times.

Also is there a way to get current seed of rand() in C? So if I use srand(seed), the random sequence will start from that point.

Name: Anonymous 2012-01-02 0:40

There's an LCG variant with that property: https://en.wikipedia.org/wiki/Lehmer_RNG

I haven't checked my math, but I think you want: Xk+i = g(i+1)·Xk mod n

Name: Anonymous 2012-01-02 0:43

>>5

how about?


static unsigned int seed;

void srand(unsigned int new_seed) {
  seed = new_seed;
}

unsigned int random(unsigned int n) {
  return (n+seed)*__REALLY_BIG_NUMBER_THAT_IS_RELATIVELY_PRIME_TO_MAX_INT__;
}

Name: Anonymous 2012-01-02 0:47

>>7
>>5
hmm, interesting. I thought you were trolling. >>6 I guess this is legit. I will try it, thanks.

Name: Anonymous 2012-01-02 0:48

>>7
Keep going bud, eventually you'll reinvent the LCG.

Name: Anonymous 2012-01-02 1:00

>>8

yeah it's a really simply technique. It makes results that look kind of random, and it visits every number exactly once, until it starts to repeat. Like if MAX_INT is 12, and you let the large number be 7, then the sequence is:


0,
7,
14-12=2,
9,
16-12=4,
11,
18-12=6,
13-12=1,
8,
15-12=3,
10,
17-12=5,
12-12=0,


0,7,2,9,4,11,6,1,8,3,10,5

It looks sorta random. cept for

0,7,2, 9
1,8,3,10

looks a little suspicious.


>>9 thanks for the reference. I must better my primitive techniques.

Name: Anonymous 2012-01-02 2:00

>>10
Yeah, you're headed in the right direction and it's a good exercise. You just need to replace MAX_INT with any modulus to generalize it (and to prevent the modulus from being a power of 2), reorder things a bit and you're basically there.

0,7,2, 9
 1,8,3,10

looks a little suspicious.


That can be helped by choosing the right constants though it will always have flaws in the distribution but it's still pretty good.

Name: Anonymous 2012-01-02 6:15

  ∧_∧       ∧_∧   ∧_∧   ∧_∧          ∧_∧
 ( ・∀・)      ( `ー´)  ( ´∀`)  ( ゚ ∀゚ )        ( ^∀^)
 (    つ┳━━∪━━∪━∪━━∪━∪━∪━━━━━┳⊂     つ
 | | | ┃This thread has peacefully ended.┃ | | |
 (__)_)┻━━━━━━━━━━━━━━━━━━━━┻ (__)_)     Thank you.

Name: Anonymous 2012-01-03 1:00

cat /dev/random

Name: Anonymous 2012-01-03 1:28

>>3
Hello, randall!!!

Name: Anonymous 2012-01-03 2:42

>>14
Bitches don't know about my rubber ducky stacking

Name: Anonymous 2012-01-03 3:54

Dammit. The old thread was a lot more interesting.

Name: Anonymous 2012-01-03 11:29

C#/.NET's Random class does exactly that, OP.

Name: Anonymous 2012-01-03 12:22

>>1
What for do you need that?

Any old linear congruential generator can do that, obviously: represent one step as a matrix over the modulo field, rise the matrix to n-th power in log(n) steps. But LGCs suck.

The state of the art in PRNG is Mersenne Twister, it can't be fast-forwarded like that apparently, but given its period you can just start with consecutive natural numbers as seeds and be sure to get non-intersecting subsequences of reasonable length. If you want to get some deterministic non-intersecting subsequences. What do you want, OP?

Name: Anonymous 2012-01-03 12:36

>>17 is a .NET and Microsoft fag, be warned.

Name: Anonymous 2012-01-03 12:37

>>19
Thanks man I was gonna hire him!!!!!!!!!!!!!!!!!!

Name: Anonymous 2012-01-03 13:21

Set ⎕RL to the value you want and then execute ? the desired number of times.

Name: Anonymous 2012-01-03 13:38

>not using chaotic equations
>2012

Name: Anonymous 2012-01-03 19:41

>>18
He probably wanted the O(1) solution that meets his needs, which was posted already.

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