473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

RAND GENERATOR IN C++

HI,

I need to generate a long random number upto 4 byte width

but my compiler is generating 2 byte int type as RAND_MAX

i want to genetrate a rand num like 123456789 that long

plz help me.

thanks for helping
Jul 19 '05 #1
3 7074
In article <f9*************************@posting.google.com> , hu*********@yahoo.com (HUNGER) wrote:
HI,

I need to generate a long random number upto 4 byte width

but my compiler is generating 2 byte int type as RAND_MAX

i want to genetrate a rand num like 123456789 that long

plz help me.

thanks for helping


Something like:

randomnumber1 = rand();
randomnumber2 = rand();
longrandomnumber = (randonumber1 << 16) | randonmumber2;

Better ways to generate 4 byte randon mumber can be found on the Internet.

Jørn Dahl-Stamnes, EDB Teamco AS
e-mail: Jo***************@nospam.novit.no (remove nospam first)
web: http://spiderman.novit.no/dahls/
Jul 19 '05 #2
> >I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long


randomnumber1 = rand();
randomnumber2 = rand();
longrandomnumber = (randonumber1 << 16) | randonmumber2;


I use this too, but I have something else:

So, It is in x86 Assembly, I convert it ;-)
--------- seed is the seed ;-) -----------
mov eax, [seed]
mul eax, 343fdh
add eax, 269ec3h
mov [seed],eax
---------first time random = seed-------
random = random * 0x343fd + 0x269ec3;
----------------------------------------
This is part of code which produce VC++ 6.0 compiler for 'rand()'
I have changed this code to return 32 bit random values
Jul 19 '05 #3
Look at a http://www.math.keio.ac.jp/matumoto/emt.html it is really good.

"HUNGER" <hu*********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
HI,

I need to generate a long random number upto 4 byte width

but my compiler is generating 2 byte int type as RAND_MAX

i want to genetrate a rand num like 123456789 that long

plz help me.

thanks for helping

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tom McCallum | last post by:
Can any tell me why rand() is such a bad pseudo-random number generator. In all the articles I have read they say that you can predict the outcome of rand() but when I used its output with NIST's...
6
by: ChasW | last post by:
given the following example: Using gcc, this compiles, runs, and outputs as expected, but on vc++.net 2003, the first number is always the same despite the time seed. #include <ctime>...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
36
by: Ben Justice | last post by:
For a program in c, I need some random numbers for a system were people are placing bets. This is not a commerical project btw. Generally, I tend to rely on things from the standard library,...
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
26
by: Gary Wessle | last post by:
Hi I need help to generate some random numbers between 2 and 8. #include <cstdlib> using std::rand; the following was out of my range, int main() {
10
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
8
by: remlostime | last post by:
i use g++ to generater rand number, now i find that the RAND_MAX is 32367 in my computer, how can i make a bigger rand number( the number is wihin in the integer(2^32-1))
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.