Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,007 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,340 people online right now. Registration is fast and FREE... Join Now!




Random number problems

 
Reply to this topicStart new topic

Random number problems, Random Num Generator giving same number ever time

jusmart
31 Mar, 2008 - 03:02 PM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 3


My Contributions
Hey,

Sorry, I forgot to mention I'm using C.

I recently wrote a program for a number guessing game.....Heres what happens when you run it. When you start it, you have 3 options, 1:have the computer "guess" (randomly generate a number between 1-100)a number and you try to guess it, 2: you pick a number and the computer "guesses" it, or 3:quit.

Now, as I said, the program all work fine, accept one important part. I used a sub-program random number generator, and called on it for the number the computer "thinks of" that you have to try and guess. Only thing is, it's the same number every time. I put in a printf statement after I called on the random generated number so I would know what it was. The program that I got the random number generator off of came directly from an old college assignment I have.

The only thing i can think of that im doing different is, I'm using bloodshed's DevC++ to write/compile this program, and I was using the college's crap when I ran the working school assignment I got the code from.(emacs)

heres the school assignment I used to get the random number generator off of......remember I only used the random number subprogram to get the number.....please dont refer any answers or info to the rest of this program.

CODE

#include <stdio.h>
#define MAX 10

int randu();

int main (void){
    int x[MAX], i,anykey;
    for (i=0; i<MAX; i++)
        x[i]=randu();
    printf("Here are your %d numbers\n",MAX);
    for(i=0; i<MAX; i++)
             printf("%d\n",x[i]);

    printf("push anykey then enter to leave\n");
    scanf("%d",&anykey);
}
int randu(){
    static int seed=17;
    seed=(25179*seed+13849)%101;
    return seed;
}


This post has been edited by jusmart: 31 Mar, 2008 - 03:08 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Random Number Problems
31 Mar, 2008 - 03:34 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,184



Thanked: 210 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
It is because your loop calls the function which executes the same equation with the same values over and over again. You go to set seed, but remember that each time you start randu you set seed back to 17.

Either way you don't need to do this... you can just use rand(). Here is an example of generating random values between 1 and 10...

cpp

#include <stdio.h>

// Include needed for rand()
#include <stdlib.h>
#define MAX 10


int main (void){
int x[MAX], i,anykey;

for (i=0; i<MAX; i++)
x[i]= (rand() % 10) + 1; // Generate number between 1 and 10

printf("Here are your %d numbers\n",MAX);
for(i=0; i<MAX; i++)
printf("%d\n",x[i]);

printf("push anykey then enter to leave\n");
scanf("%d",&anykey);
}


Notice the use of rand and the modulus operator. This will produce values that fall between 0 and 9 and then you add the 1 so that you will never get zero and will get 10 as a value.

Hope that works for you. Enjoy! smile.gif
User is online!Profile CardPM
+Quote Post

Entropicvamp
RE: Random Number Problems
31 Mar, 2008 - 04:52 PM
Post #3

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 14


My Contributions
QUOTE(jusmart @ 31 Mar, 2008 - 04:02 PM) *

Hey,

Sorry, I forgot to mention I'm using C.

I recently wrote a program for a number guessing game.....Heres what happens when you run it. When you start it, you have 3 options, 1:have the computer "guess" (randomly generate a number between 1-100)a number and you try to guess it, 2: you pick a number and the computer "guesses" it, or 3:quit.

Now, as I said, the program all work fine, accept one important part. I used a sub-program random number generator, and called on it for the number the computer "thinks of" that you have to try and guess. Only thing is, it's the same number every time. I put in a printf statement after I called on the random generated number so I would know what it was. The program that I got the random number generator off of came directly from an old college assignment I have.

The only thing i can think of that im doing different is, I'm using bloodshed's DevC++ to write/compile this program, and I was using the college's crap when I ran the working school assignment I got the code from.(emacs)

heres the school assignment I used to get the random number generator off of......remember I only used the random number subprogram to get the number.....please dont refer any answers or info to the rest of this program.

CODE

#include <stdio.h>
#define MAX 10

int randu();

int main (void){
    int x[MAX], i,anykey;
    for (i=0; i<MAX; i++)
        x[i]=randu();
    printf("Here are your %d numbers\n",MAX);
    for(i=0; i<MAX; i++)
             printf("%d\n",x[i]);

    printf("push anykey then enter to leave\n");
    scanf("%d",&anykey);
}
int randu(){
    static int seed=17;
    seed=(25179*seed+13849)%101;
    return seed;
}


I don't know if it will help you, but I know how to do it for C++. I had to do a Random Guessing Number Game in C++. For my one class a while ago if it will help you out, let me know okay and I'll post it for you.
User is offlineProfile CardPM
+Quote Post

jusmart
RE: Random Number Problems
31 Mar, 2008 - 08:16 PM
Post #4

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 3


My Contributions
Hey,
I know I don't have to use the sub-program and I understand that when I recall the subprogram again, its using the same numbers because the seed is set to 17, but I do know that it's possible to run a sub-program number generator that gives dif numbers every time it's called on , as long as the program is running. I know it's possible, because I have done it, I'm just trying to do this other program with the sub-program and not the randu function, because I was asked particularly not to use anything but a sub program.

I'm not positive but I do believe the way it's supposed to work is, when it generates it's first number, say 17, it's supposed to save the generated number as the seed so that the next number is different. Now, if you were to close then re-open the program again, you would get the same numbers in the same order. Thats how it used to work, the one i did in the past that is. If I had that one, I wouldnt be needing help. But anyways, it is possible with the sub-program, thats what i'm needing help with.

Thanks to everyone helping out, but I need these very particular guidlines. Must be ran by a sub-program, and must not use other functions like randu. Again, thanks for the input but this can only be in C, not C++.

Also remember, this is going in a different program that the one I mentioned, I only posted it for the generator reference. I care less about the array or the exit strategy of that program. Thanks again for all the help and input.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Random Number Problems
31 Mar, 2008 - 09:39 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,184



Thanked: 210 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
No perhaps you misunderstood me, I am not saying each time the program is ran it gives you the same numbers, I am talking about every time you call randu from your main function, your calculation is being set to 17 again. Do the math!

CODE

25179*seed = 25179 * 17 = 428043
428043 + 13849 = 441892
441892 % 101 = .... guess what.... 17!


So seed is set back to 17 again and the cycle repeats each time you call randu()!

Try changing 13849 to 13840 and you will see it start generating random numbers.

I don't know where you got that equation to start with, but the equation's answer is the same as the previous seed. wink2.gif
User is online!Profile CardPM
+Quote Post

jusmart
RE: Random Number Problems
1 Apr, 2008 - 04:52 AM
Post #6

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 3


My Contributions
Hey, I didnt mean to come off like a dick about aqnything, but i understood that everytime it was called it would re-start at 17, just had no clue that the equation they would have used would come out to be 17.....Sorry for the mis-understanding and my huge lack of knowledge in this area....Thanks a ton for everyones efforts. I now have it working properly thanks to you guys!

This post has been edited by jusmart: 1 Apr, 2008 - 04:52 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month