12 Replies - 363 Views - Last Post: 22 January 2013 - 09:42 AM Rate Topic: -----

#1 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Random number generator acting strange...?

Posted 21 January 2013 - 10:48 PM

Ok, so here is my program:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <ctime>

int main()
{
	int number;
	srand((unsigned)time(0));
	printf("NAME SURNAME\n");
	number = 100000000 + rand() % 999999999;
	printf("%d", number);
	
	getch();
	return (0);
}


This produces a random 9 digit number, which I want, but seems to go up by 1 ever second or something? I am after completely random! like 111111111 the first try and 534567648 the second try, but it is like 100000564 the first try then do it right away again and it is 100000572 and so on and so forth. I have scowered the internet trying to find helpful guides to random number generators and nothing has come up.

I was unable to use mt_rand(), which sounded like it could give me what I want, but it kept saying it was not identified and I couldn't find whatever library was needed for it...? I am on visual studios 2010 if that makes any difference...

Thanks for any help!

This post has been edited by jimblumberg: 22 January 2013 - 06:48 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Random number generator acting strange...?

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Random number generator acting strange...?

Posted 21 January 2013 - 11:09 PM

Well, I don't know what seems to be happening with your program results, but here are my 10 results after execution of the program 10 times...after commenting out your Name part.

865955609
143448459
949013252
603001827
410500081
471235534
460992520
655819986
265321095
856004729


regards,
Raghav

This post has been edited by raghav.naganathan: 21 January 2013 - 11:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Random number generator acting strange...?

Posted 21 January 2013 - 11:17 PM

lol well what in the world... I am still getting my weird numbers slowly going up buy 1... I suppose they are still "random" and still count eh? Haha
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Random number generator acting strange...?

Posted 21 January 2013 - 11:20 PM

Well, I don't know if this might help, but I didn't use <conio.h> and getch() in my program.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#include <iostream>
#include <ctime>

int main()
{
  int number;
  srand((unsigned)time(0));
  //printf("NAME SURNAME\n");
  number = rand() % 999999999;
  printf("%d", number);

return (0);
}


You can try it out and see if it works.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Random number generator acting strange...?

Posted 21 January 2013 - 11:26 PM

Yeah its still slowly climbing up numbers :( But I had to keep conio.h and getch() otherwise my program instantly exits once it runs. And I need it to be 9 digit number, so I had to keep 100000000 + in front of rand()
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Random number generator acting strange...?

Posted 22 January 2013 - 01:03 AM

View PostBawnawgwa, on 21 January 2013 - 11:26 PM, said:

Yeah its still slowly climbing up numbers :(/>/> But I had to keep conio.h and getch() otherwise my program instantly exits once it runs. And I need it to be 9 digit number, so I had to keep 100000000 + in front of rand()


You have exceeded the RAND_MAX value for your system. (Sounds like 16 bit Turbo C). On a 64 bit system it's up to 11 digits long.

You can print out what it is on your system by including stdlib.h and printing up %d RAND_MAX.

You may need to take two smaller random numbers, and then add them together as a string or an unsigned long int, to get the size you want.

Might consider moving up to a current compiler. I like Pelles C for Windows (does console stuff also, of course). It's free.

This post has been edited by Adak: 22 January 2013 - 01:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Random number generator acting strange...?

Posted 22 January 2013 - 01:08 AM

BUt 9 digits should be ok? I am on 64 bit system so it should work out right? Also, since I got it to kinda give me random numbers, I have found nothing on how to insert dashes to generate xxx-xx-xxxx. I have tried strings but can never get strings to be identified. is VS2010 to out of date? I can get 2012 I suppose (free through school :P).
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Random number generator acting strange...?

Posted 22 January 2013 - 02:17 AM

This is where I get off helping you, since the pattern you want matches the pattern of a social security number.

You should re-think what you're trying to do. Depending on where you live, assisting in fraud with a computer, could be a serious crime.
Was This Post Helpful? 0
  • +
  • -

#9 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Random number generator acting strange...?

Posted 22 January 2013 - 09:13 AM

Haha sorry, it is for an assignment! The text for the assignment says "you work at whatever government agency to produce new social security cards for people" I paraphrased, but yeah it is just supposed to give random 9 digits in SSN form. No real SSN.

And I don't mean to have people do my homework for me, its just our very first assignment for introductory computer engineering course is make 7 programs. We had 3 days of class before this assignment is due and did we learn one bit of programming? No... not even how to begin. I am pretty impressed with myself on the ones I have completed (4 of 7), but on getting the dashes into the 9 digit number... I am completely out of ideas and our instructor just left us in the dark...

And its introductory course! To be taught to students who haven't programmed! Ugh... annoying...
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Random number generator acting strange...?

Posted 22 January 2013 - 09:31 AM

You could make a 3 element array for SSN and populate it with three digits in element 0, two digits in element 1 and four digits in element 2. Then outputting it is easy.
Was This Post Helpful? 0
  • +
  • -

#11 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Random number generator acting strange...?

Posted 22 January 2013 - 09:35 AM

Hmmm... could you possibly tell me the basic structure of an "element array" as far as coding for it goes? I will try diggin some stuff on online for it but google has been a let down so far with my coding questions :(

Hmmm... could you possibly tell me the basic structure of an "element array" as far as coding for it goes? I will try diggin some stuff on online for it but google has been a let down so far with my coding questions :(
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Random number generator acting strange...?

Posted 22 January 2013 - 09:39 AM

	int SSN[3];


Was This Post Helpful? 0
  • +
  • -

#13 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Random number generator acting strange...?

Posted 22 January 2013 - 09:42 AM

Ok thanks! I will try it when I get home and post back my results
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1