C++ code for large random numbers

  • dmx1999
  • Newbie
  • Newbie
  • dmx1999
  • Posts: 12

Post 3+ Months Ago

Hello
I wonder how i can write a C code that creates large random numbers, from 1 to 6400000

I have this:

Code: [ Select ]
int ran;
srand( (unsigned)time( NULL ) );
ran =(int) N * rand() / (RAND_MAX + 1.0) + 1;
  1. int ran;
  2. srand( (unsigned)time( NULL ) );
  3. ran =(int) N * rand() / (RAND_MAX + 1.0) + 1;


where N is the range that i need, which is 6400000 in this case

the code seems to have a problem that N has to be smaller then 32767 and cannot go higher then that...
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

What type did you declare N to be?
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

not as random, but how about making a random number 1-6400 and 1-1000 then putting the two together?
  • Kasracer
  • Banned
  • Banned
  • Kasracer
  • Posts: 26
  • Loc: Maryland

Post 3+ Months Ago

1 of the limitations of rand() is the fact that it can only work with ints.

However, if you only need to go up to 64,000 why not use an unsigned int? That can reach up to 65,535 and is still an integer, so I think it still works with rand().

That or you could create a function that seeds rand(), then runs rand() twice and multiplies them together. You just hade to make sure neither can go higher than 64,000.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 159 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.