LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-17-2008, 10:17 AM   #1
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
-1 (inverse)


Hi, i'm having a play with random number generators and was looking at this.

My question is, how do you do 'inverse' in C?
I've found some functions for 'inverse modulus', but i don't think thats right.

Heres a template i'm playing with:
Code:
#include <stdio.h>
#include <stdlib.h>

int prng(int x, int a, int c, int m)
{
	/*
		Based upon 'Inversive Congruential Generator'
		x - last / start number (seed)
		a - increment
		c - multiplier
		m - modulus
	*/
	return (int)((1/(a*x))+c)%m;
}

int main(int argc, char *argv[])
{
	int i;
	int tot = 20;
	int x = 3;
	int a = 5;
	int c = 3;
	int m = 255;
	
	printf("nums: ");
	for(i=0;i<tot;i++)
	{
		x = prng(x, a, c, m);	//	x, a, c, m
		printf("%d ", x);
	}
	printf("\n");
	
	return(0);
}
 
Old 10-17-2008, 10:22 AM   #2
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
oops, this seems to be a solution (well the results match my calculator):

Code:
#include <math.h>
	printf("1: %.8f\n", pow(1,-1));
	printf("1: %.8f\n", pow(2,-1));
	printf("1: %.8f\n", pow(3,-1));
	printf("1: %.8f\n", pow(4,-1));
 
Old 10-17-2008, 10:23 AM   #3
IsharaComix
Member
 
Registered: Sep 2008
Location: Raleigh
Distribution: Ubuntu 8.10
Posts: 88
Blog Entries: 1

Rep: Reputation: 15
x^(-1) is equal to 1/x

Just put whatever you want to inverse under a one. That should work if C does math like normal people...
 
Old 10-17-2008, 10:40 AM   #4
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
yes thats what i was trying first 1/x, i think my problem is lying in the choice of numbers, because all my results currently are repetitive, so i'm looking at this and attempting to figure out a way to generate from a single seed...
 
Old 10-17-2008, 06:30 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You will need to use float numbers at some point because an inverse of an int will always result in zero. (1/3 = 0 remainder 1)
 
  


Reply



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
How can I inverse mouse movement directions? edesapa Linux - Hardware 6 06-17-2006 04:12 AM
Texmaker & inverse-search setting Blue Jacket Linux - Software 1 04-19-2006 03:27 PM
LXer: The Inverse Extension Design Pattern LXer Syndicated Linux News 0 12-21-2005 11:16 PM
atitvout works but colors are inverse mehlkelm Linux - Software 3 12-12-2004 05:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:13 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration