ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

Okay, sorry gents (and any ladies) for this question. But I just couldn't stomp out the problem. And I have zero friends that program in C, so no help there. Well I was trying to make a dice rolling program. It takes the number of sides feeds it to genNum() (take a look at the code) and then uses rand() to generate a number, and then feeds it down an if(). Probably a slow and painful way to go about this, but it's the only way I could think of. This code compiles, but it never achieves a number above 41, or below 3.

Code:

#include <stdio.h>
#include <stdlib.h>
void genNum( int );
main()
{
int dieSide = 0;
printf("\nHow many sides does the die have? ");
scanf("%d", &dieSide);
genNum( dieSide );
return 0;
}
void genNum( int SidesOfDie )
{
int dieNum = 0;
dieNum = rand();
if( (dieNum <= SidesOfDie) && (dieNum != 0) )
{
printf("The number is %d\n\n", dieNum);
}
else
{
genNum( SidesOfDie );
}
}

Any help debugging this would be much apprechiated (wow, spelling errors suck). And no, don't answer this question if you feel I needed google (which I checked).

I don't know much about C, more about C++, but i believe rand() returns a number between 0 and an internal number called RAND_MAX. The sequence of numbers is what's called "pseudorandom": for a given start number it always gives out the same sequence. I suggest that in main, before you call genNum(), you insert the following line:

srand(time(NULL));

This takes a "seed" for the random number generator from the CPU clock, so at least you won't always get the same sequence. You might also want to consider changing the way genNum() works: it might be better to write

The operator % gives you the remainder after dividing one integer by another. You'll need to think about the maths a bit and check it's right (i didn't think too hard to see if it's right i'm afraid!) but it might help....

by Gethyn int dieNum = rand() % ((RAND_MAX / sidesOfDie) - (RAND_MAX % sidesOfDie))

im not sure whether that works or not but it might be better to think about it like this, you want to generate a number between 1 and sidesOfDie, so (rand() % sidesOfDie) returns the remainder of dividing rand() by sidesOfDie which will obviously be between 0 and sidesOfDie - 1, add 1 to it and youve got your answer.

1 + (rand() % sidesOfDie)

btw your recursive method could bring you a few nasty emails from the sysadmin.

I might be thinking about this wrong, but if you just put rand() % sidesOfDie, and sidesOfDie doesn't go exactly into RAND_MAX, then you'll be skewing the probabilities. Let's see if i can explain that:
say RAND_MAX is 10 (obviously far too small but...)
and sidesOfDie is 3
so, if rand() returns 0 you get 0, 1 you get 1, etc
for rand() output 4 you get 1, 5 you get 2, and so on.
The problem arises when rand() returns 10. In this case you'll get 1. However, there are no matching outputs for 2 or 3 (which are valid results from the die)
So in total, the probabilities are :
0 = 4/10 (corresponding to 0, 3, 6, 9)
1 = 4/10 (from 1, 4, 7, 10)
2 = 3/10 (from 2, 5, 8)

This obviously isn't right. If RAND_MAX is big enough compared to sidesOfDie then the effect is negligible, but it's not entirely correct...

When you say my recursive method is going to get me into trouble, please excuse my ignorance, but could you explain? (i take it you're not referring to my somewhat less than excellent programming? )

the recusive comment was to kneeless(sorry that wasnt clear, i have aspergers syndrome and tend to think people know everything i know), in regard to him calling getnum recursivly, with a terminating condition that relied on a random number.

as far as the probability skewing goes you are totally correct but as you say sidesOfDie is gonna be much smaller than RAND_MAX so the probabilities are gonna vary by amounts in the magnitude of 4.7*10^-10 (1/randmax) which is really insignificant compared to the 'psuedoness' of the random number generator. if you wanna do some real accurate probability you should use a mathematical library like NAG for fortran, and not use the normal random number generator.

oh and your probabilities are 4/10(0,3,6,9), 3/10(1,4,7), 3/10(2,5,8)

lets pretend that sizeOfDie is 6 and that rand() always returns a number bigger than 6. so we call getNum, dieNum>6(previous sentance) and 6>1 so we call getNum again, and the same thing happens, so we call it again, and again and again and again, until we run out of stack space and generate some sort of signal. hopefully we havnt killed the system while were doing it.

a recursive function is one which calls itsself, all recursive functions have terminating conditions, yours was when dieNum was within the correct bounds but the problem is this condition occurs randomly so may never occur at all.

hey guys (and gals if any), i was reading through this thread hoping i might find an answer to a problem i was having with rand()..

i'm trying to get a random 5 digit integer (i'm going to use it in a crack authentication mechanism, this is a CGI written in C ) the integer will be a 'session id'... i've tried to beat this horse but cant quite get it to work.. every thing i've tried always returns the same thing.

if someone could tell me how to make it 32 digits long, that would be even better but i don't NEED that much.

Quote:

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

/* omitted.. this is lenghthy... */

long idnum;
idnum = rand()%50000+10024;

/* omitted again.. */

i had also tryied using a char string, and atol(), but that worked with less than favorable results so i am not gonna bother posting it. i admit i'm a HORRIBLE coder

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.