473,396 Members | 2,013 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,396 software developers and data experts.

random numbers

I would like to know how to get different random numbers within a range from 1 to 6, to include both numbers 1 and 6.
I have used this code:
n = rand() % 6 + 1;
but I end up with the same number .
How do I get to give me different numbers and not the same number consecutively ?
Oct 31 '07 #1
3 1394
bartonc
6,596 Expert 4TB
I would like to know how to get different random numbers within a range from 1 to 6, to include both numbers 1 and 6.
I have used this code:
n = rand() % 6 + 1;
but I end up with the same number .
How do I get to give me different numbers and not the same number consecutively ?
That will depend on which programming language that you are using.
This looks like DB, but it's hard for those of us who write in other languages to tell.
Oct 31 '07 #2
sicarie
4,677 Expert Mod 4TB
I would like to know how to get different random numbers within a range from 1 to 6, to include both numbers 1 and 6.
I have used this code:
n = rand() % 6 + 1;
but I end up with the same number .
How do I get to give me different numbers and not the same number consecutively ?
One of the more interesting areas of programming are the random numbers. These numbers aren't actually random, per se, but are more "pseudo-random" in that they were designed to be able to be replicated based on the 'seed' value that it initialized with. This means that if you have the same seed value, you can call rand() as many times as you want, and you will still be able to close your program, come back to it in a few days, and run it again with the exact same results. This means that with the same seed value (I believe yours is set to the default), you will get the same first number each time.

This is useful in scientific programs, but if you want a different random number each time, I would recommend (depending on the language - I'm going to write this to be C specific, but I'm pretty sure you could make this assumption for most programming languages and find similar functions) setting the seed value (in C/C++ this is done with srand() ) to the current time.

Expand|Select|Wrap|Line Numbers
  1. srand( time( NULL) );
  2. //your call to rand here should now vary, make sure to run it a few times!
  3.  
If you're not in C/C++ and you can't figure out how to change the seed value, post again and we'll do our best to track it down for you (though we'll probably start by asking what you Google searched ;).
Nov 1 '07 #3
try times it by the time thats what i am working on in vb
Nov 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
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: 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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.