Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:54:36+00:00 2026-05-24T21:54:36+00:00

I’m very new to C. I want to write C-code that I will wrap

  • 0

I’m very new to C. I want to write C-code that I will wrap in Cython.
This is my C-code (a random-value generator based on a seed):

#include <math.h>

typedef struct {
    int seed;
    int bits;
    int max;
    double fmax;
    int value;
} rGen;

rGen rGen_Init(int seed, int start, int bits) {
    rGen gen;
    int max = pow(2, bits);
    gen.seed    = seed % max;
    gen.bits    = bits;
    gen.max     = max;
    gen.fmax    = (double) max;
    gen.value   = start % max;
    return gen;
}

double rGen_Next(rGen gen) {
    int value = gen.value;
    value = (gen.seed << gen.bits) * value / (gen.bits + 1);
    value = pow(value, 4);
    value = value % (gen.max - 1);
    gen.value = value;

    return (double) value / gen.fmax;
}

Doing a little check if the value attribute of the rGen instance does change:

#include "pyran.c"
#include <stdio.h>

int main() {
    rGen gen = rGen_Init(1000, 200, 32); // Create an initialized struct-instance
    int i;
    for(i = 0; i < 10; i += 1) {
        rGen_Next(gen);
        printf("%i\n", gen.value);
    }
    return 0;
}

The output is:

200
200
200
200
200
200
200
200
200
200

The value of the value attribute does not change.
Why does it behave like this ? Do I have to use pointers ? And if so, how do I use them ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:54:37+00:00Added an answer on May 24, 2026 at 9:54 pm

    rGen_Next takes its argument by value, modifies the copy and discards it. To actually modify it, take a pointer argument.

    double rGen_Next(rGen* gen) {
        int value = gen->value;
        value = (gen->seed << gen->bits) * value / (gen->bits + 1);
        value = pow(value, 4);
        value = value % (gen->max - 1);
        gen->value = value;
    
        return (double) value / gen->fmax;
    }
    

    Call it like this: rGen_Next(&gen); if gen is a variable with automatic storage duration.
    Also: It is good C practice to have an init function that takes a pointer to the object actually being initialized.
    This way it is up to the user how the structure is allocated.

    bool rGen_Init(rGen* gen, int seed, int start, int bits) {
        int max = pow(2, bits);
        gen->seed    = seed % max;
        gen->bits    = bits;
        gen->max     = max;
        gen->fmax    = (double) max;
        gen->value   = start % max;
    
        // omit the return if there is no chance of failure, 
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.