LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-26-2012, 02:35 AM   #1
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Seed PRNG from stdin in C


I have the following setup:

I record audio from a radio using 'arecord -c 1 -r 48000 -t raw' at 48000 Hz mono 8-bit raw PCM. I hash this at a ratio of 2:1 and end up with 24000 bytes per second of random data.

I want to use the SIMD MT PRNG:
http://www.math.sci.hiroshima-u.ac.j...FMT/index.html
and seed it somehow from the hashed radio output.

I have rewritten (not very well) the test program to try to do this:
PHP Code:

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

#include "SFMT.h"

#define BLOCK_SIZE64 131072

int main(int argcchar argv[])
{
    
unsigned int i;
    
unsigned int ocount;
    
unsigned int sno;
    
unsigned int iseed;
    
uint32_t buffer;
    
uint64_t array64 [BLOCK_SIZE64];
    
sfmt_t sfmt;

    
// make sure block size is large enough
    
if (BLOCK_SIZE64 sfmt_get_min_array_size64(&sfmt))
    {
        
fprintf(stderr,"ERROR: array size %d < %d\n"BLOCK_SIZE64sfmt_get_min_array_size64(&sfmt));
        exit(
1);
    }

    
// parse arg
    
if (!= argc)
    {
        
fprintf(stderr,"Usage:\n%s input_seed_size(bytes) output_count(MB) seed_every_n_outputs\n"argv[0]);
        exit(
1);
    }

    
sscanf(argv[1], "%u", &iseed);
    
sscanf(argv[2], "%u", &ocount);
    
sscanf(argv[3], "%u", &sno);

    
// malloc
    
buffer = (uint32_t *) malloc (iseed);
    if ( 
NULL == buffer )
    {
        
fprintf(stderr"ERROR: not enough memory\n");
        exit(
2);
    }

    for (
i=0ocounti++)
    {
        
// get seed and initialize every num outputs
        
if (== (sno))
        {
            
fread(bufferiseed1stdin);
            
sfmt_init_by_array(&sfmtbufferiseed);
        }

        
// fill
        
sfmt_fill_array64(&sfmtarray64BLOCK_SIZE64);

        
// output
        
fwrite(array64BLOCK_SIZE648stdout);
    }
    
    return 
0;

The problem is that arecord gets a buffer overflow once in a while:
Code:
overrun!!! (at least -1351234668987.974 ms long)
Probably I need some type of buffering, or to somehow adjust the input and output rates so that it doesn't overflow. I would still like maximum output from the PRNG. I'm not sure how to do the calculations or buffer it properly.

Thanks.
 
Old 10-27-2012, 04:08 AM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Original Poster
Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Ok, maybe it wasn't the best idea. I fixed it by just feeding the radio output in /dev/random and using /dev/urandom as the seed for the PRNG. I modified the program a bit:

PHP Code:
#include <stdio.h>
#include <stdlib.h>

#include "SFMT.h"

int main(void)
{
    
// const
    
const unsigned int inchunk32 1024 4;
    const 
unsigned int outchunk64 1024 1024 8;

    
// ! init
    
uint32_t buffer32 [inchunk32];
    
uint64_t array64 [outchunk64];
    
sfmt_t sfmt;

    
// make sure block size is large enough
    
if (outchunk64 sfmt_get_min_array_size64(&sfmt))
    {
        
fprintf(stderr,"ERROR: array size %u < %d\n"outchunk64sfmt_get_min_array_size64(&sfmt));
        exit(
1);
    }
    
    
// IO loop
    
while (fread(buffer32inchunk324stdin))
    {
        
// init
        
sfmt_init_by_array(&sfmtbuffer32inchunk32);

        
// fill
        
sfmt_fill_array64(&sfmtarray64outchunk64);

        
// output
        
fwrite(array64outchunk648stdout);
    }
    
    return 
0;

I suppose I will mark this solved.
 
Old 05-08-2013, 12:53 PM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Original Poster
Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Ok, I have found a much better solution after looking at the problem again. I guess I just wasn't thinking clearly. This now is capable of around 600 MB/s.

PHP Code:
#include <stdio.h>

#include "SFMT.h"

int main (int argcchar argv[])
{
    
// parse argc
    
if (!= argc)
    {
        
fprintf (stderr"Usage:\n%s input_bytes output_MiB\nExample:\n%s 24000 3000\n"argv[0], argv[0]);
        return 
1;
    }

    
// parse argv
    
unsigned int inbytesoutmib;
    
sscanf (argv[1], "%u", &inbytes);
    
sscanf (argv[2], "%u", &outmib);

    
// const
    
const unsigned int inchunk32 inbytes 4;
    const 
unsigned int outchunk64 1024 1024 8;

    
// ! init
    
uint32_t buffer32 [inchunk32];
    
uint64_t array64 [outchunk64];
    
sfmt_t sfmt;
    
unsigned int i;

    
// make sure block size is large enough
    
if (outchunk64 sfmt_get_min_array_size64 (&sfmt))
    {
        
fprintf (stderr,"ERROR: array size %u < %d\n"outchunk64sfmt_get_min_array_size64 (&sfmt));
        return 
1;
    }

    
// IO loop
    
while (fread (buffer32inchunk324stdin))
    {
        
// init
        
sfmt_init_by_array (&sfmtbuffer32inchunk32);

        for (
0outmibi++)
        {
            
// fill
            
sfmt_fill_array64 (&sfmtarray64outchunk64);

            
// output
            
fwrite (array64outchunk648stdout);
        }
    }

    return 
0;

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do you seed for torrents? silvyus_06 General 2 02-20-2011 02:33 PM
prng library in linux? tony_lincon Linux - Newbie 12 06-17-2010 02:25 AM
PRNG is not set while running sshd saurin Programming 1 10-10-2009 10:32 PM
What should I seed? conanm4 Linux - General 5 08-07-2006 11:18 AM
about PRNG dummyagain Programming 0 10-25-2004 03:40 AM


All times are GMT -5. The time now is 12:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration