Click here to Skip to main content
16,016,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all, I have been through the forums and Google and have found only complicated setups for random number generators that are more advanced than rand(). My problem with rand() is that I can't seem to change the RAND_MAX, and even when I do by using #undef and #define followed by the integer max, it still doesn't go beyond 32768.

Here's what I'm looking for:
- I would like a random function that I can access by including the header file (whether it's enclosed by <> or "").
- It should have a maximum of whatever integer can handle or long long or even floating point numbers.
- I would also like it to be able to generate negative numbers, if possible. I may have a workaround for this but it would be nice if the function generated it by itself.
- I would also like to have a range of numbers that the generating functions stays within.

Please guide me as to non-complicated steps to achieve this if I want to run this on a Windows 64-bit computer using my favorite IDE Code::Blocks. Thanks in advance for your help.

What I have tried:

I have been through the forums, not just this one, and a multitude of Google searches.
Posted
Updated 14-Jul-16 20:27pm
Comments
Mohibur Rashid 15-Jul-16 0:04am    
What is your compiler?
I just have tested this on linux 64 bit
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, n;
time_t t;

printf("%d\n", RAND_MAX);
n = 5;
/* Intializes random number generator */
srand((unsigned) time(&t));

/* Print 5 random numbers from 0 to 49 */
for( i = 0 ; i < n ; i++ )
{
printf("%d\n", rand());
}

return(0);
}

the result is :
2147483647
584940180
862659951
1917044237
210835866
1005714980


2147483647 = (2^31)-1
and your max is
32768 = (2^14)
I am guessing your RAND_MAX = 32767
Aamir Yousafi 15-Jul-16 2:09am    
I am using the GCC compiler on a Windows 64-bit system. Yes, I've heard that the C implementation on my type of computer sets RAND_MAX to 32768 and there's no way to change it. Your computer is a Linux. I've heard that C works most perfectly in Linux. It's not practical or affordable for me to purchase a Linux-based OS yet. And my computer is a laptop, Acer V5-571g, probably not dual-bootable.
Mohibur Rashid 15-Jul-16 3:06am    
Well, if you have interest in working on linux environment,(I am not sure if you are student) without buying a new one, I would give you a small tips. Install virtual box. Its free. And install the linux(Make sure not to install graphics version). Get putty and ssh ..
Aamir Yousafi 15-Jul-16 9:03am    
but virtual box is a Windows application, right? How would I work on C from that environment?
Mohibur Rashid 16-Jul-16 2:31am    
First install virtual box. Then create a Virtual Computer. Then install linux in your virtual computer, don't forget to install openssh-server in your virtual box. then use putty.. and don't expect to learn everything in the comment. please try to take hint, start learning.

You can't change RAND_MAX because it's a constant, and its implementation dependant: it can be set to the maximum value that can be stored in a signed integer on the system the compiler is targeting (but it doesn't have to be, it can be smaller): on Windows based systems it is set to 32767 for compatibility reasons which are probably unnecessary today.
If you want a bigger range of random values, then that's difficult in a C based environment as libraries available to C programs are pretty old these days and are unlikely to be updated.
One way to get a bigger random number would be to use rand repeatedly and shift the bits into a bigger number as suggested here: How to generate large random numbers C - Stack Overflow[^]
But...do note that with a maximum value of 32767 you will need to shift your values by 15 bits each time, not 16 as they show or you will get huge "gaps" in the values caused by the zeroed bit 15 each time.
 
Share this answer
 
Comments
CPallini 15-Jul-16 2:23am    
5.
Mohibur Rashid 15-Jul-16 3:02am    
that's a fascinating idea. I was going to suggest to get visual studio express. But it would also be great...
If you can use C++ then have a look at this page: random - C++ Reference[^].
 
Share this answer
 
Comments
Aamir Yousafi 15-Jul-16 9:05am    
Well, CPallini, I'm not using C++ right now so that's why I'm not going to get into this right now. I'll save this for the future (near future, probably). Thanks.
There are a number of relatively simple algorithms out there that you can write on your own, for example, the linear congruential generator, or LCG. You can poke around a bit. I asked the latest version of ChatGPT to provide C source code. I did have to stipulate that I didn't want the rand() function involved. I, too, use GCC on Windows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900